Chuck
Chuck

Reputation: 53

Using Django: How to serve a file for download?

Let me start by saying I have spent a lot of energy on this and any input is very valuable to me at this point. Now, onto my question:

What I have working: My website provides a simple interface for users to upload a file in which they will be given a key. The file is saved to the server and the key is generated using a modern, secure encryption algorithm. These details are not important to the question at hand.

What I want to implement: On a download page, the user can enter the key and the file will be downloaded to their computer. Want I want to know is how to provide a user with this ability to download an uploaded file. To put it simply, I just want to allow users to download a file in the MEDIA_ROOT folder.

I am using class-based views, so my urls are:

urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
from fileupload.views import FileDownloadView, GetFileView

urlpatterns = patterns('',
    url(r'^key/?$', FileDownloadView.as_view(), name='getkey'),
    url(r'^download/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}, GetFileView.as_view()),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Please provide me with the basic idea on what functions I should use in my views to actually download the file. I am still in development so I want to take advantage of Django's ability to serve files, not Apache's. I will consider other options if I go into production. Whatever code I need to post, I can post.

Thank you!

Upvotes: 2

Views: 3148

Answers (3)

Chuck
Chuck

Reputation: 53

Along with the url file I have shown in the original question, all I needed to do from here was to provide an HttpResponseRedirect('/path/of/my/media/file')

Very simple!

Upvotes: 0

Zulu
Zulu

Reputation: 9275

Basically I told you, Django should not serve files in production.

After is it possible yes:

You can serve a file tree with django.conf.urls.static.static(), as a view in your urls.py. Example:

urlpatterns = patterns('',
    ...
    static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    ...
)

Or use whitenoise, a package for server static file with Python web apps. It is very simple to use, only change your wsgi.py file like this:

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

And use 'whitenoise.django.GzipManifestStaticFilesStorage' as STATICFILES_STORAGE.

Upvotes: 0

Brent Washburne
Brent Washburne

Reputation: 13148

If the key grants them access to the file, why not have the key be the file name? That is, when the file is uploaded, create a long key (say 64 characters long) and rename the file to the key name. Then simply serve up that file using the static.serve method when it is requested.

The benefits of this approach are: 1) You don't need to map the key to the file in the database, and 2) you don't need to validate the key when retrieving the file. If the file name is found, the key is valid.

Upvotes: 1

Related Questions