将字符串传递给收件人列表时,'list'对象没有属性'encode'

时间:2017-02-13 03:26:26

标签: django attributeerror classview

我遇到了一个Django应用程序的问题,我试图使用基于类的视图执行send_mail。每次我提交表单时,都会收到此错误:/ contact /'list'对象的AttributeError没有属性'encode'。不知道该怎么做。

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic.edit import FormView
from django.core.mail import send_mail
from django.http import request

from .forms import ContactForm


def index(request):
return render(request, "main/index.html")


def about(request):
    return render(request, "main/about.html")

def thanks(request):
    return render(request, "main/thanks.html")


class ContactView(FormView):
    template_name = "main/contact.html"
    form_class = ContactForm
    success_url = '/thanks/'

def form_valid(self, form):
    form.send_email()
    return super(ContactView, self).form_valid(form)

这是我的ContactForm

forms.py

from django import forms
from django.core.mail import send_mail

class ContactForm(forms.Form):

    subject = forms.CharField(max_length=75)
    message = forms.CharField(widget=forms.Textarea)
    sender = forms.EmailField()

    def send_email(self):
        # Write some code for sending an email
        # pass
        mar = []
        x = "email@icloud.com"
        mar.append(x.encode('ascii'))
        print(type(x))
        send_mail(
            self.cleaned_data['subject'],
            self.cleaned_data['message'],
            self.cleaned_data.get('sender', 'to@mail.com' ),
            mar,
            fail_silently=False,
        )

堆栈跟踪:

Quit the server with CONTROL-C.
Internal Server Error: /contact/
Traceback (most recent call last):
   File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-         packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
   File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
   File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
   File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args,  **callback_kwargs)
  File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-packages/django/views/generic/edit.py", line 183, in post
    return self.form_valid(form)
  File "/Users/ivorybook/code/medmen-django-aptitude-test-355ce458b36c/main/views.py", line 27, in form_valid
    form.send_email()
  File "/Users/ivorybook/code/medmen-django-aptitude-test-355ce458b36c/main/forms.py", line 22, in send_email
    fail_silently=False,
  File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-packages/django/core/mail/message.py", line 342, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site- packages/django/core/mail/backends/smtp.py", line 100, in send_messages
    new_conn_created = self.open()
  File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 67, in open
    self.connection.login(self.username, self.password)
  File  "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/smtplib.py", line    720, in login
    initial_response_ok=initial_response_ok)
  File "/Users/ivorybook/anaconda2/envs/aptitude/lib/python3.5/smtplib.py", line 637, in auth
    authobject(challenge).encode('ascii'), eol='')
AttributeError: 'list' object has no attribute 'encode'
[12/Feb/2017 20:24:00] "POST /contact/ HTTP/1.1" 500 119658

0 个答案:

没有答案