How to create file download links in Django?
In order to create a download link, we need to create a Django view that would serve the files:
# views.py
import mimetypes
...
def download_file(request):
# fill these variables with real values
fl_path = ‘/file/path'
filename = ‘downloaded_file_name.extension’
fl = open(fl_path, 'r’)
mime_type, _ = mimetypes.guess_type(fl_path)
response = HttpResponse(fl, content_type=mime_type)
response['Content-Disposition'] = "attachment; filename=%s" % filename
return response
...
Once you've done this, add a line to urlpatterns
in urls.py
, which references the view.
# urls.py
...
path(‘<str:filepath>/‘, views.download_file)
...
How it works?
It works because we send the following HTTP header to a browser:
Content-Disposition: attachment; filename
It tells a browser to treat a response as a downloadable file. Have you noticed that we also include Content-Type
header? This one tells what kind of file we are sending to the browser or in other words its mime type. If the header is not set Django will set it to text/html
. mimetypes.guess_type
is a handy function which tries to guess the mime type of the file, however if you know the mime type of your file(s) beforehand, it is better to set this manually.