Having Django serve downloadable files
For the "best of both worlds" you could combine S.Lott's solution with the xsendfile module: django generates the path to the file (or the file itself), but the actual file serving is handled by Apache/Lighttpd. Once you've set up mod_xsendfile, integrating with your view takes a few lines of code:
from django.utils.encoding import smart_strresponse = HttpResponse(mimetype='application/force-download') # mimetype is replaced by content_type for django 1.7response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)response['X-Sendfile'] = smart_str(path_to_file)# It's usually a good idea to set the 'Content-Length' header too.# You can also set any other required headers: Cache-Control, etc.return response
Of course, this will only work if you have control over your server, or your hosting company has mod_xsendfile already set up.
EDIT:
mimetype is replaced by content_type for django 1.7
response = HttpResponse(content_type='application/force-download')
EDIT: For nginx
check this, it uses X-Accel-Redirect
instead of apache
X-Sendfile header.
A "download" is simply an HTTP header change.
See http://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment for how to respond with a download.
You only need one URL definition for "/download"
.
The request's GET
or POST
dictionary will have the "f=somefile.txt"
information.
Your view function will simply merge the base path with the "f
" value, open the file, create and return a response object. It should be less than 12 lines of code.
For a very simple but not efficient or scalable solution, you can just use the built in django serve
view. This is excellent for quick prototypes or one-off work, but as has been mentioned throughout this question, you should use something like apache or nginx in production.
from django.views.static import servefilepath = '/some/path/to/local/file.txt'return serve(request, os.path.basename(filepath), os.path.dirname(filepath))