Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.

For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt

And on the server, I know that all downloadable files reside in a folder "/home/user/files/".

Is there a way to make Django serve that file for download as opposed to trying to find a URL and View to display it?

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code:-

from django.views.static import serve 

filepath = '/some/path/to/local/file.txt' 

return serve(request, os.path.basename(filepath),os.path.dirname(filepath))

Browse Categories

...