gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:19:31 25 4
gpt4 key购买 nike

我花了好几天的时间试图弄清楚这个问题,但绝对碰壁了。我已经研究并阅读了有关此问题的所有 SO 帖子,但最终还是需要一些帮助。

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

我的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'

最佳答案

然后问题出在您的 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'

关于python - Django Forms 和 'tuple' 对象没有属性 'rsplit' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40778038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com