我正在将 Django 项目从 1.3 迁移到 1.5。
一个发送电子邮件的应用程序现在无法在我的开发机器中的 1.5 中运行。我收到此错误:
/envia_activacion/ 'ascii' 编解码器的 UnicodeEncodeError 无法在位置 37 对字符 u'\xf3' 进行编码:序数不在范围内(128)
Traceback:
File "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/django/clones/migracion/libs/my_apps/emailing/views.py" in envia_email
48. mailer.msg.send()
File "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/mail/message.py" in send
255. return self.get_connection(fail_silently).send_messages([self])
File "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/mail/backends/smtp.py" in send_messages
95. sent = self._send(message)
File "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/mail/backends/smtp.py" in _send
113. force_bytes(message.as_string(), charset))
File "/home/django/.virtualenvs/migracion/lib/python2.6/site-packages/django/core/mail/message.py" in as_string
169. g.flatten(self, unixfrom=unixfrom)
File "/usr/lib/python2.6/email/generator.py" in flatten
84. self._write(msg)
File "/usr/lib/python2.6/email/generator.py" in _write
109. self._dispatch(msg)
File "/usr/lib/python2.6/email/generator.py" in _dispatch
135. meth(msg)
File "/usr/lib/python2.6/email/generator.py" in _handle_multipart
201. g.flatten(part, unixfrom=False)
File "/usr/lib/python2.6/email/generator.py" in flatten
84. self._write(msg)
File "/usr/lib/python2.6/email/generator.py" in _write
109. self._dispatch(msg)
File "/usr/lib/python2.6/email/generator.py" in _dispatch
135. meth(msg)
File "/usr/lib/python2.6/email/generator.py" in _handle_text
178. self._fp.write(payload)
Exception Type: UnicodeEncodeError at /envia_activacion/
Exception Value: 'ascii' codec can't encode character u'\xf3' in position 37: ordinal not in range(128)
我的代码是这样的:
plaintext = get_template('emailing/activacion.txt')
htmly = get_template('emailing/activacion.html') //html template with utf-8 charset
d = Context({
'code': code,
'version': sVersion,
'activacion': activacion,
'distribuidor': distribuidor,
'contacto': contacto,
'departamento': departamento,
'empresa': empresa
})
asunto = u'Código activación %s - V%s - Code: %s' % (nombre_destinatario, version, code)
text_content = plaintext.render(d)
html_content = htmly.render(d)
msg = EmailMultiAlternatives(asunto, text_content, remitente, para, [remitente], headers={'Reply-To': remitente})
msg.attach_alternative(html_content, "text/html")//This is what makes it fail
msg.send()
这在 django 1.3 中有效,在 django 1.4 中不知道。
调试,我看到失败的文本是unicode:
> /usr/lib/python2.6/email/generator.py(178)_handle_text()
-> self._fp.write(payload)
(Pdb) s
UnicodeEncodeError: UnicodeE...ge(128)')
> /usr/lib/python2.6/email/generator.py(178)_handle_text()
-> self._fp.write(payload)
(Pdb) payload
u'Apreciado cliente, le indicamos los c\xf3digos necesarios ...'
我正在使用 python 2.6.5。我已经查看了这张看起来几乎相同的票https://code.djangoproject.com/ticket/19107但在 django 1.5 中该补丁已经存在并且我仍然收到此错误。
我真的很感激任何帮助。谢谢