To make Python Django serve downloadable files, we can return a response with some special values.
For instance, we write
from django.utils.encoding import smart_str
response = HttpResponse(content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(path_to_file)
return response
to create a HttpResponse
instance and assign it to response
.
Then we set the Content-Disposition
response header with
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
And then we set the X-Sendfile
header to the path of the file with
response['X-Sendfile'] = smart_str(path_to_file)
And then we return the response
in our view.