Django联系表单:TypeError:" to"参数必须是列表或元组

时间:2018-01-05 04:26:10

标签: django forms views

我正在尝试将我的电子邮件联系表单用于我的Django网站。 我在浏览器中收到如下错误:

  

异常类型:TypeError异常值:"到"论证必须是一个   列表或元组异常   位置:/home/Gassymule/Skeletonv3.1/myvenv/lib/python3.6/site-packages/django/core/mail/message.py   在 init ,第264行

特别强调

try:
     send_mail(name, company, phone, subject, message, from_email, ['kkeeper.ch@gmail.com'])

来自我的views.py但我不完全确定它需要我修复什么。我从中获取的Tutorial网站使用了这种确切的格式。

views.py

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views.generic import TemplateView
from website.forms import ContactForm
from django.core.mail import send_mail, BadHeaderError


def email(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            name = form.cleaned_data['name']
            company = form.cleaned_data['company']
            phone = form.cleaned_data['phone']
            try:
                send_mail(name, company, phone, subject, message, from_email, ['kkeeper.ch@gmail.com'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('thanks')
    return render(request, "email.html", {'form': form})


def thanks(request):
    return HttpResponse('Thank you for your message.')

以下是我的联系表单模板中的相关代码。

email.html

            <form action="" method="post">
                {% csrf_token %}
                <ul class="contactList">
                    <li id="name" class="contact">{{ form.name }}</li>
                    <li id="company" class="contact">{{ form.company }}</li>
                    <li id="email" class="contact">{{ form.from_email }}</li>
                    <li id="phone" class="contact">{{ form.phone }}</li>
                    <li id="subject" class="contact">{{ form.subject }}</li>
                    <li id="message" class="contact">{{ form.message }}</li>
                </ul>
                <input id=submitb type="submit" value="Submit">
            </form>
        </div>

这是我的forms.py

forms.py

from django import forms


class ContactForm(forms.Form):

    from_email = forms.EmailField(required=True)
    subject = forms.CharField(required=False,)
    message = forms.CharField(widget=forms.Textarea(attrs={'cols': 43, 'rows': 8}))
    name = forms.CharField(required=True)
    phone = forms.CharField(required=False)
    company = forms.CharField(required=False)



    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        self.fields['from_email'].widget.attrs['placeholder'] = 'Email'
        self.fields['subject'].widget.attrs['placeholder'] = 'Subject'
        self.fields['message'].widget.attrs['placeholder'] = 'Message'
        self.fields['name'].widget.attrs['placeholder'] = 'Name'
        self.fields['phone'].widget.attrs['placeholder'] = 'Phone'
        self.fields['company'].widget.attrs['placeholder'] = 'Company'

我错过了什么?

2 个答案:

答案 0 :(得分:3)

send_mail中你需要传递这样的参数。

(subject, message, from_email, recipient_list)

其中recipient_list是发送邮件的email_addresses的列表或元组。

你正在传递,其他论点:只做这个

send_mail(subject, message, from_email, ['kkeeper.ch@gmail.com'])

它会正常工作。

答案 1 :(得分:0)

send_email方法的签名是

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

https://docs.djangoproject.com/en/2.0/topics/email/#send-mail

从代码中删除前三个参数:name, company, phone