I have an application using Django and, when the user clicks in a button in certain page, it calls a view method from the server side, which is responsible for generating a file in memory (.docx) and sending it to the user's browser. This file is not stored anywhere in the server. This works fine at localhost, when I run it in my own machine.
But, when I uploaded the code to PythonAnywhere and I click in the button, I am redirected to the page Something went wrong, presenting an Unhandled Exception.
Looking at the error.log file, I see this:
2020-08-26 00:32:58,175: SystemError: <built-in function uwsgi_sendfile> returned a result with an error set
2020-08-26 00:32:58,175: File "/home/capitale/.virtualenvs/myenv/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 148, in __call__
2020-08-26 00:32:58,176: response = environ['wsgi.file_wrapper'](response.file_to_stream, response.block_size)
The code resposible for generating the file and returning it to browser is the one below (I've ommited some parts to focus in what I think are the most important ones):
from docxtpl import DocxTemplate
import io
dev generateFile(request):
document = DocxTemplate(filePath)
document.render({
...
})
doc_io = io.BytesIO()
document.save(doc_io)
doc_io.seek(0)
return FileResponse(doc_io, as_attachment=True, filename='file.docx')
Any idea on what is going on and how do I fix it?