I looked at the various questions similar to mine, but I could not find anything a fix for my problem.
In my code, I want to serve a freshly generated excel file residing in my app directory in a folder named files
excelFile = ExcelCreator.ExcelCreator("test")
excelFile.create()
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="test.xls"'
return response
So when I click on the button that run this part of the code, it sends to the user an empty file. By looking at my code, I can understand that behavior because I don't point to that file within my response...
I saw some people use the file wrapper (which I don't quite understand the use). So I did like that:
response = HttpResponse(FileWrapper(excelFile.file),content_type='application/vnd.ms-excel')
But then, I receive the error message from server : A server error occurred. Please contact the administrator.
Thanks for helping me in my Django quest, I'm getting better with all of your precious advices!
excelFile.file
? Also, have you tried running this with the developmentrunserver
andDEBUG = True
? That should get more useful feedback. – BiedermeierDEBUG = True
in yoursettings.py
? – BiedermeierFileWrapper
fromdjango.core.servers.basehttp
in your views.py because that would result in an error like you mentioned. – Synthesizeexcept
as you are as that can hide problems. I will continue thinking on this. – Synthesizemanage.py runserver
? If not can you? This would allow you to see errors on the console that you can't see from the page since you are getting just a base error page with no details. For example, when testing yourviews.py
I got aTemplateDoesNotExist
exception due to my not having your template. – SynthesizeExcelCreate
from a public module, or something written by you (or in house)? Does excelFile.file provide a handle to the file, or is it the data of the file? FileWrapper expects a handle, so if that's not what excelFile.file is it could definitely cause the behavior you are seeing. – Synthesize