Sometimes you want Django to return the contents of a file as a result. In the sample you often see, f
is used as a file-like object.
res = HttpResponse(f.read(), content_type="text/csv")
However, this is inefficient (memory) because it reads the entire file at once. This is simply because the first argument of HttpResponse
can be an iterator
res = HttpResponse(f, content_type="text/csv")
And it is sufficient.
Recommended Posts