Django download a file
You missed underscore in argument document_root. But it's bad idea to use serve
in production. Use something like this instead:
import os
from django.conf import settings
from django.http import HttpResponse, Http404
def download(request, path):
file_path = os.path.join(settings.MEDIA_ROOT, path)
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
return response
raise Http404
Django view to download a file from server
In order to download a file, you need to return a FileResponse
to the user. However, calling an external function that returns a FileResponse
won't work because you're not actually returning the FileResponse
to the user, in your case the user only receives the render(request, 'generate_mo.html', {'misses':misses,})
so that won't download the files.
You can't download several files one after the others, so I suggest putting them all in a .zip or .tar file so that you can download them as only one file, and only need to return one FileResponse
.
As you also need to render your template, what you can do is redirect to your download_mo
view on template loading so that your file is downloaded while your template is rendered.
Now, for your download_mo
view, just replace your HttpResponse
with a FileResponse
:
from django.http import FileResponse
def download_mo(request,file):
path_to_file = os.path.realpath(file)
response = FileResponse(open(path_to_file, 'rb'))
file_name = file[5:]
response['Content-Disposition'] = 'inline; filename=' + file_name
return response
Download a file with Django
- You need to read that file.
- Serve it using
HttpResponse
along with proper content type.
Here's some sample code:
content = open("uploads/something.txt").read()
return HttpResponse(content, content_type='text/plain')
This should serve a text file.
But as you described, on some browser, it will not ask to download the file, rather, it would show it in the browser. If you want to show a download prompt, use this:
response = HttpResponse(open("uploads/something.txt", 'rb').read())
response['Content-Type'] = 'text/plain'
response['Content-Disposition'] = 'attachment; filename=DownloadedText.txt'
return response
However, please note that it might be a better idea to serve static contents or uploaded files via nginx or the reverse proxy of your choice. Sending large files through Django might not be the most optimum way of doing that.
How to download file using django as backend and nginx
i have found another way, there is a library which makes everything alot easier named as django_downloadview.
Here is the link to library https://pypi.org/project/django-downloadview/
and here is the link to documentations https://django-downloadview.readthedocs.io/en/latest/
It solved the problem for me now for creating a download link with an id to download file from storage lets see what happens in the future.
Download file in Nginx-Django with X-Accel-Redirect
The problem was that the nginx did not expect to read files from the provided directory. When this directory was changed to /var/wwww
, it worked as expected.
Related Topics
Get Name of Current Script in Python
How to Check If an Ip Is in a Network in Python
Plot a Histogram Such That Bar Heights Sum to 1 (Probability)
Websocket VS Rest API for Real Time Data
Check If a File Is Open in Python
Generate a Random Derangement of a List
How to Use Youtube-Dl from a Python Program
Schedule Python Script - Windows 7
Store Mouse Click Event Coordinates with Matplotlib
Progress Indicator During Pandas Operations
Why am I Getting Importerror: No Module Named Pip ' Right After Installing Pip
Looping Over All Member Variables of a Class in Python
Number of Days Between 2 Dates, Excluding Weekends
Parameter Substitution for a SQLite "In" Clause
Bin Elements Per Row - Vectorized 2D Bincount for Numpy
How Many Concurrent Requests Does a Single Flask Process Receive