我想在郵件正文中發送我的發佈數據。在郵件正文中發送POST數據 - Django
我想要做這樣的事情,但是body dict顯然不是正確的格式(這會返回錯誤'dict' object has no attribute 'splitlines'
)。完成此操作的最佳做法是什麼?
from django.core.mail import EmailMessage
def send_mail(request):
body = {
'name': request.POST['name'],
'phone': request.POST['phone'],
'email': request.POST['email'],
'appointment prefs': request.POST['appt'],
'message': request.POST['message'],
}
email = EmailMessage(
'Website Inquiry',
body,
to=['[email protected]']
)
email.send()
這裏的錯誤:
Request Method: POST
Request URL: http://localhost:8000/send_mail
Django Version: 1.11
Python Version: 2.7.10
Installed Applications:
['apps.cphh',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'smartfields']
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.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/kristaprokopczyk/Documents/pet_health_harmony/pet_health_harmony/apps/cphh/views.py" in send_mail
31. email.send()
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/message.py" in send
342. return self.get_connection(fail_silently).send_messages([self])
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/backends/smtp.py" in send_messages
111. sent = self._send(message)
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/backends/smtp.py" in _send
125. message = email_message.message()
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/message.py" in message
301. msg = SafeMIMEText(self.body, self.content_subtype, encoding)
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/message.py" in __init__
214. MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/mime/text.py" in __init__
30. self.set_payload(_text, _charset)
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/message.py" in set_payload
224. for l in payload.splitlines()
Exception Type: AttributeError at /send_mail
Exception Value: 'dict' object has no attribute 'splitlines'
你在哪兒叫'splitlines'?請徹底分享您的代碼,以便我們提供幫助。 – ozgur
我不使用Django,但可以將該字典轉儲爲json並嘗試? –
請顯示您的真實密碼。這甚至不會編譯;你有''''包裝你的'body'字典而不是'{}'。 –