【问题标题】:python + django+ EmailMultiAlternatives Template' object has no attribute 'encodepython + django+ EmailMultiAlternatives Template'对象没有属性'encode
【发布时间】:2013-08-05 23:24:16
【问题描述】:

我正在通过以下方式发送电子邮件:

EmailMultiAlternatives() 

send() 方法被调用时;我在某个基类中遇到错误。

lib/email/encoders.py
'Template' object has no attribute 'encode'

异常位置:C:\Python27\lib\email\encoders.py 在 encode_7or8bit 中,第 73 行

Python 可执行文件:C:\Python27\python.exe

错误路径:

>   return msg.send(fail_silently)

C:\Python27\lib\email\mime\text.py in __init__
>  self.set_payload(_text, _charset)

C:\Python27\lib\email\message.py in set_payload
>   self.set_charset(charset)

C:\Python27\lib\email\message.py in set_charset
>  cte(self)

C:\Python27\lib\email\encoders.py in encode_7or8bit
>  orig.encode('ascii')

谁能知道这个错误?

更新 使用以下代码帮助我解决了我的问题。

fail_silently=True

t = get_template(template_name)
html_part = t.render(context)
msg = EmailMultiAlternatives(subject,
                                 t,
                                 sender,
                                 recipients,
                                 bcc=bcc)
msg.attach_alternative(html_part, "text/html")

return msg.send(fail_silently)

【问题讨论】:

  • 看起来有一个Template() 对象正在被发送;不是字符串。模板未呈现为字符串。
  • 我在 1.3 django 中使用了相同的代码,我今天升级了它,我收到了这个错误。这是我的电子邮件代码。查看更新。
  • 你是如何解决这个错误的?
  • 你可以在更新标题下看到工作代码。

标签: python django email send


【解决方案1】:

使用 django.template.loader 中的 get_template 方法时,我收到以下错误

错误:“模板”对象的 AttributeError 没有属性“分割线”

对我有用的解决方案

from django.template.loader import render_to_string

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 2015-03-28
    • 1970-01-01
    • 2018-10-16
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多