Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:SystemError:<;内置函数uwsgi_sendfile>;返回了一个带有错误集的结果_Python_Django_Pythonanywhere_Python Docx - Fatal编程技术网

Python Django:SystemError:<;内置函数uwsgi_sendfile>;返回了一个带有错误集的结果

Python Django:SystemError:<;内置函数uwsgi_sendfile>;返回了一个带有错误集的结果,python,django,pythonanywhere,python-docx,Python,Django,Pythonanywhere,Python Docx,在localhost上,它工作得非常好,但在pythonany上,它不再工作了。当我按下一个应该下载word文档的按钮时,我得到了以下错误消息:SystemError:返回了一个带有错误集的结果。 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:]

在localhost上,它工作得非常好,但在pythonany上,它不再工作了。当我按下一个应该下载word文档的按钮时,我得到了以下错误消息:SystemError:返回了一个带有错误集的结果。 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'对象没有属性'write'”:


我认为您可能以错误的顺序创建了各种流--请尝试以下方法:

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')
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')