Having Django serve downloadable files

后端 未结 15 1808
野的像风
野的像风 2020-11-22 05:46

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

15条回答
  •  一整个雨季
    2020-11-22 06:50

    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 serve
    filepath = '/some/path/to/local/file.txt'
    return serve(request, os.path.basename(filepath), os.path.dirname(filepath))
    

提交回复
热议问题