gpt4 book ai didi

python - 在电子邮件中使用 Attach_alternative 时出现 Django 1.5 UnicodeEncodeError

转载 作者:行者123 更新时间:2023-12-01 05:46:45 29 4
gpt4 key购买 nike

我正在将 Django 项目从 1.3 迁移到 1.5。

一个发送电子邮件的应用程序现在在我的开发机器上的 1.5 版本中无法运行。我收到此错误:

/envia_activacion/处的UnicodeEncodeError“ascii”编解码器无法对位置 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 中该补丁已经存在,但我仍然收到此错误。

相关: unicode error in python

我真的很感激任何帮助。谢谢

最佳答案

您的有效负载是一个 unicode 实例(请注意,Django 力求仅在内部使用 unicode),而它应该是 str 实例。您有责任传递(最好是正确编码的)str。这很简单 FWIW:

# I assume you use utf-8 everywhere 
text_content = plaintext.render(d).encode("utf-8")
html_content = htmly.render(d).encode("utf-8")

关于python - 在电子邮件中使用 Attach_alternative 时出现 Django 1.5 UnicodeEncodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15831209/

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