Django: SystemError: <内置函数uwsgi_sendfile>返回的结果是一个错误集。

问题描述 投票:2回答:1

在localhost上,它工作得很好,但在pythonanywhere上,它就不能工作了。当我按下一个应该下载word文档的按钮时,我得到了这个错误信息。SystemError: returned a result with an error set. views.py:

def downloadWord(request, pk):
order = Order.objects.get(pk=pk)
order_items = order.order_items.all()




date = f'{order.date}'
d =date[8:]
y = date[:4]
m = date[5:7]
date = f'{d}.{m}.{y}'


context={

    'order_items': order_items,
    'order': order,
    'date': date

}

byte_io = BytesIO()
tpl = DocxTemplate(os.path.join(BASE_DIR, 'media/word_documents/order.docx'))
tpl.render(context)
tpl.save(byte_io)
byte_io.seek(0)
data = dict()

return FileResponse(byte_io, as_attachment=True, filename=f'order_{order.title}.docx')

我还尝试使用werkzeug的FileWrapper,但它说 "AttributeError: 'FileWrapper' object has no attribute 'write'"。

from werkzeug.wsgi import FileWrapper


def downloadWord(request, pk): 
order = Order.objects.get(pk=pk) 
order_items = order.order_items.all()

date = f'{order.date}'
d =date[8:]
y = date[:4]
m = date[5:7]
date = f'{d}.{m}.{y}'


context={

    'order_items': order_items,
    'order': order,
    'date': date

}

byte_io = BytesIO()
byte_io = FileWrapper(byte_io)
tpl = DocxTemplate(os.path.join(BASE_DIR, 'media/word_documents/order.docx'))
tpl.render(context)
tpl.save(byte_io)
byte_io.seek(0)


return FileResponse(byte_io, as_attachment=True, filename=f'order_{order.title}.docx')
python django pythonanywhere python-docx
1个回答
1
投票

我想你可能是以错误的顺序创建了这些不同的流 -- 试试这个。

tpl = DocxTemplate(os.path.join(BASE_DIR, 'media/word_documents/order.docx'))
tpl.render(context)

byte_io = BytesIO()
tpl.save(byte_io)
byte_io.seek(0)

return FileResponse(FileWrapper(byte_io), as_attachment=True, filename=f'order_{order.title}.docx')
© www.soinside.com 2019 - 2024. All rights reserved.