Django Forms和'tuple'对象没有属性'rsplit'错误

时间:2016-11-24 04:26:02

标签: python django django-forms django-views

我认真地试图解决这个问题已经过了几天,并且完全碰到了一堵砖墙。我已经研究并阅读了这个问题上的所有SO帖子,但最终可以使用一些帮助。

我正在尝试连接一个简单的联系表单,下面是我提交表单时抛出相同'tuple' object has no attribute 'rsplit' error的代码的最新版本。

我的Django版本是1.9并使用Python3.5

def contact(request):
    title = 'Contact Us'
    title_align_center = True
    form = ContactForm(request.POST or None)

    if form.is_valid():
        form_email = form.cleaned_data.get('email')
        form_message = form.cleaned_data.get('message')
        form_full_name = form.cleaned_data.get('full_name')
        subject = 'Site Contact Form'
        from_email = settings.DEFAULT_FROM_EMAIL
        recipient_list = [from_email, 'charles@studiorooster.com']
        message = '%s: %s via %s' % (
            form_full_name,
            form_message,
            form_email)
        # some_html_message = ''' <h1>hello</h1> '''

        email = EmailMessage(
            subject,
            message,
            form_email,
            recipient_list,
            ['roosteromg@gmail.com'],
            reply_to=[from_email],
            headers={'Reply-To': from_email},
        )
        email.send()

    context = {
        'form': form,
        'title': title,
        'title_align_center': title_align_center,
    }
    return render(request, 'pages/contact.html', context)

回溯

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8001/contact/

Django Version: 1.9
Python Version: 3.5.1
Installed Applications:
['accounts',
 'comments',
 'pages',
 'posts',
 'search',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_summernote']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Users/rooster/Documents/Development/boston_intercom/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/Users/rooster/Documents/Development/boston_intercom/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/rooster/Documents/Development/boston_intercom/pages/views.py" in contact
  80.         email.send()

File "/Users/rooster/Documents/Development/boston_intercom/venv/lib/python3.5/site-packages/django/core/mail/message.py" in send
  292.         return self.get_connection(fail_silently).send_messages([self])

File "/Users/rooster/Documents/Development/boston_intercom/venv/lib/python3.5/site-packages/django/core/mail/message.py" in get_connection
  250.             self.connection = get_connection(fail_silently=fail_silently)

File "/Users/rooster/Documents/Development/boston_intercom/venv/lib/python3.5/site-packages/django/core/mail/__init__.py" in get_connection
  36.     klass = import_string(backend or settings.EMAIL_BACKEND)

File "/Users/rooster/Documents/Development/boston_intercom/venv/lib/python3.5/site-packages/django/utils/module_loading.py" in import_string
  15.         module_path, class_name = dotted_path.rsplit('.', 1)

Exception Type: AttributeError at /contact/
Exception Value: 'tuple' object has no attribute 'rsplit'

1 个答案:

答案 0 :(得分:0)

然后问题出在您的settings.py文件中,其中包含默认邮件设置,正如您在追溯中看到的那样, EMAIL_BACKEND 设置可能是问题所在。

这些设置应该是这样的:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL =  'youremail@gmail.com'