python - 发送附件 'file'时Django 1.5错误对象没有属性 '__getitem__'

标签 python django

我正在尝试通过电子邮件发送附件,pdf 但我收到以下错误 'file' object has no attribute '__getitem__'

这是我的代码。

created_pdf = open(filename)

from_email = settings.DEFAULT_EMAIL
email = EmailMessage('KenyaBuzz Tickets for %s'%seat1.movie, "There's a pdf attached with the tickets, please print it and provide it at...", from_email,
        [seat1.user.email],
        )
email.attach(filename, created_pdf, 'application/pdf')
email.send()

最佳答案

attach 的第二个参数应该是数据,而不是文件对象。

email.attach(filename, created_pdf.read(), 'application/pdf')

但是最好使用attach_file,它直接接受一个文件名:

email.attach_file(filename)

关于python - 发送附件 'file'时Django 1.5错误对象没有属性 '__getitem__',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31320239/

相关文章:

python - 连接错误 : HTTPSConnectionPool SSLError(SSLError(1, u'[SSL : CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl. c :661)'), ))

python - Tkinter Canvas 缩放 + 移动/平移

python - Kmeans 算法的特征缩放

django - 如何使登录表单在 Django 的整个站点上可用

python - 验证 django 内联表单集

python - 对混合有字母的实数列表进行排序

python - python中的Fire store用随机数据填充字段

python - 理解 python 中的 lambda 并使用它来传递多个参数

django - 根据 "Two Scoops of Django"在 Django 中存储静态/模板文件的位置

python - Django: 'module' 对象没有属性 'index'