我使用xlwt
模块创建了excel对象。但是,当我尝试通过电子邮件将其作为附件EmailMessage()
尝试通过“AttributeError: 'int' object has no attribute 'lower'
进行序列化和错误时。如何在Django中通过电子邮件发送此附件?
File "/home/git/bot_server/bot_server/bot_data/views.py", line 590, in email_export
email.send()
File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/core/mail/message.py", line 274, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/core/mail/backends/console.py", line 34, in send_messages
self.write_message(message)
File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/core/mail/backends/console.py", line 17, in write_message
msg = message.message()
File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/core/mail/message.py", line 241, in message
msg = self._create_message(msg)
File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/core/mail/message.py", line 299, in _create_message
return self._create_attachments(msg)
File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/core/mail/message.py", line 312, in _create_attachments
msg.attach(self._create_attachment(*attachment))
File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/core/mail/message.py", line 339, in _create_attachment
attachment = self._create_mime_attachment(content, mimetype)
File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/core/mail/message.py", line 327, in _create_mime_attachment
Encoders.encode_base64(attachment)
File "/usr/lib/python2.7/email/encoders.py", line 45, in encode_base64
encdata = _bencode(orig)
File "/usr/lib/python2.7/email/encoders.py", line 31, in _bencode
hasnewline = (s[-1] == '\n')
File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/http/response.py", line 192, in __getitem__
return self._headers[header.lower()][1]
AttributeError: 'int' object has no attribute 'lower'
电子邮件功能:
def email_export(export, user_email):
subject = 'Stuff'
from_address = 'noreply@foo'
body = "Hello"
recip = [user_email]
email = EmailMessage(subject, body, from_address, recip)
email.attach('community_support_excel.xls', export, 'application/ms-excel')
email.send()
一些excel功能:
def export_xls(queryset):
response = HttpResponse(mimetype='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename=mymodel.xls'
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet("MyModel")
more stuff.....
wb.save(response)
return response
答案 0 :(得分:0)
问题是我试图通过电子邮件发送HTTPResponse对象作为不允许的附件。我要做的是将excel表存储在用io.BytesIO()创建的对象中。然后使用export_object.getvalues()发送附件。