Django returns the contents of the file as an HTTP response

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

Django returns the contents of the file as an HTTP response
Regularly monitor the HTTP response of the web server
Output the output result of sklearn.metrics.classification_report as a CSV file
An example of a mechanism that returns a prediction by HTTP from the result of machine learning
How to see the contents of the Jupyter notebook ipynb file
Simulation of the contents of the wallet
Understand the function of convolution using image processing as an example
Process the contents of the file in order with a shell script
Loop the For statement in reverse in an HTML file on Django
Calculate the editing distance as an index of pronunciation similarity [python]
Get the formula in an excel file as a string in Python
Easy encryption of file contents (Python)
Understand the contents of sklearn's pipeline
Get the attributes of an object
See the contents of Kumantic Segumantion
The story of the "hole" in the file
The meaning of ".object" in Django
Visualize the response status of the census 2020
If you just want to get the dump file of the server, it was convenient to build an http server
Open an Excel file in Python and color the map of Japan
The story of creating Botonyan that returns the contents of Google Docs in response to a specific keyword on Slack
A simple mock server that simply embeds the HTTP request header in the body of the response and returns it.
I checked the contents of docker volume
I tried the asynchronous server of Django 3.0
The story of an error in PyOCR
How to check the version of Django
The story of making Python an exe
Check the existence of the file with python
The importance of Lint as Pythonista thinks
The story of making an immutable mold
Read all the contents of proc / [pid]
[Python] Get the character code of the file
Consider the description of docker-compose.yml (Django + MySQL ③)
[Python3] Understand the basics of file operations
[Bash] While read, pass the contents of the file to variables for each column
Save an array of numpy to a wav file using the wave module
Django template reads Makdown and reStructuredText written in an external file as HTML