14👍
Try passing the fsock
iterator as a parameter to HttpResponse()
, rather than to its write()
method which I think expects a string.
response = HttpResponse(fsock, mimetype=...)
See http://docs.djangoproject.com/en/dev/ref/request-response/#passing-iterators
Also, I’m not sure you want to call close
on your file before returning response
. Having played around with this in the shell (I’ve not tried this in an actual Django view), it seems that the response
doesn’t access the file until the response
itself is read. Trying to read a HttpResponse
created using a file that is now closed results in a ValueError: I/O operation on closed file
.
So, you might want to leave fsock
open, and let the garbage collector deal with it after the response is read.
15👍
Could it be that the file contains some non-ascii characters that render ok in production but not in development?
Try reading the file as binary:
fsock = open(file_path,"rb")
- [Django]-Get count of related model efficiently in Django
- [Django]-Django Multiple Authentication Backend for one project
- [Django]-Gunicorn Connection in Use: ('0.0.0.0', 5000)
1👍
Try disabling “django.middleware.gzip.GZipMiddleware” from your MIDDLEWARE_CLASSES in settings.py
I had the same problem, and after I looked around the middleware folder, this middleware seemed guilty to me and removing it did the trick for me.
- [Django]-"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.
- [Django]-Django: Filter a Queryset made of unions not working
- [Django]-Django multiple template inheritance – is this the right style?