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 ...
After that add a line to
urls.py which references the view.
# urls.py ... path(‘<str:filepath>/‘, views.download_file) ...
So, all the magic is done by including a
Content-Disposition: attachment; filename HTTP header. 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
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.