"至"参数必须是Django中的列表或元组

时间:2018-01-27 18:26:45

标签: python django

我有一个像这个问题主题的错误:

"to" argument must be a list or tuple

我想创建一个电子邮件表单,但我找不到错误。也许有人帮助我?

我的文件是: 的 forms.py

from django import forms

class WyslijEmail(forms.Form):
    name = forms.CharField(max_length=25)
    subject = forms.CharField(max_length=255)
    email = forms.EmailField()
    message = forms.CharField(required=True, widget=forms.Textarea)

views.py

from django.shortcuts import render, redirect
from django.core.mail import send_mail, BadHeaderError
from .forms import WyslijEmail


def kontakt(request):
    if request.method == 'GET':
        form = WyslijEmail()
    else:
        form = WyslijEmail(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            subject = form.cleaned_data['subject']
            email = form.cleaned_data['email']
            message = form.cleaned_data['message']
            try:
                send_mail(name,subject,email,message, ['admin@gmail.com',])
            except BadHeaderError:
                return render('Niewlasciwe cos tam')
            return redirect('sukces')
    return render(request,'bajki/kontakt.html', {'form':form})

def sukces(request):
    return render('Sukces udało sie')

kontakt.html

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <p><input type="submit" value="Wyślij wiadomość"></p>
</form>

我必须定义什么来解决这个问题?

Os所以我更新了我的文件,现在它们看起来像这样: views.py

from django.shortcuts import render, redirect
from django.core.mail import send_mail
from .forms import WyslijEmail


def kontakt(request):
    sent = False
    if request.method == 'POST':
        form = WyslijEmail(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            send_mail(subject, message, from_email,['login@gmail.com'])
            sent = True
    else:
        form = WyslijEmail()
    return render(request,'bajki/kontakt.html', {'form':form, 'sent': sent})

但这是第二个问题。当我尝试从页面发送电子邮件时,系统回复了我这样的错误:

SMTPAuthenticationError at /kontakt/
(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials h63sm1453929ljf.36 - gsmtp')

我更改了Google帐户中的设置以接受所有电子邮件。我不知道如何解决它。

4 个答案:

答案 0 :(得分:0)

您的 send_mail 方法应该有四个参数。

send_mail(subject, message, from_email, recipient_list)
EX:
subject == string
message == string
from_email == string
recipient_list == list

您似乎还有其他参数&#39; name &#39;。你还没有提到from_email参数。

答案 1 :(得分:0)

以下是Django Documentation

send_mail的结构
send_mail(subject, 
          message, 
          from_email, 
          recipient_list, 
          fail_silently=False, 
          auth_user=None, 
          auth_password=None, 
          connection=None, 
          html_message=None)

您应该相应地更改send_mail函数中的参数。

答案 2 :(得分:0)

我相信您的问题出在send_mail()函数上。 我建议您将form.clean_data保存在变量中以实现可重用性 即cd = form.clean_data. 然后为send_mail正确传递参数,并将cd ['to']包装在列表中

send_mail(subject, message, 'admin@myblog.com', [cd['to']])

答案 3 :(得分:-1)

更新后的代码中的问题是to_email参数包含一个列表。除了编写['login@gmail.com']之外,您还需要用逗号将['login@gmail.com',]写入列表中。