Having Django serve downloadable files

后端 未结 15 1910
野的像风
野的像风 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条回答
  •  猫巷女王i
    2020-11-22 06:35

    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.

提交回复
热议问题