Django return download file
Update the urls. Open the browser and execute the following URL that will display the file. If the user clicks on the download link, the CF. If this file exists in the base location of the app, then the following dialog box will appear. The user can open the file in the browser or the document viewer before the download or download the file directly without opening it by selecting the Save File option and pressing the OK button.
Adding a download option for text and PDF files in the Django application was shown in this tutorial using a simple script. The new Django users will get an idea to add a download option without and with a download link in the Django app after reading this tutorial.
I am a trainer of web programming courses. I needed to use a django. HttpResponse , as seen below:. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 5 years, 3 months ago. Active 1 month ago. Viewed 36k times. ODFTemplate 'test. How do I make it start downloading as file instead? Improve this question. Viktor Viktor 3, 3 3 gold badges 32 32 silver badges 57 57 bronze badges. You mean to say that you have created an word file which you need to pass to Front End so that Front end user should able to download it?
Wanare exactly — Viktor. Maybe after the file is generated, if it's publicly accessible from your web server without Django code, authorisation, etc you could send a Redirect response.
For the "best of both worlds" you could combine S. 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. Basically, Django still handles serving the file but does not load the whole thing into memory at once.
This allows your server to slowly serve a big file without ramping up the memory usage. Again, S. Lott's X-SendFile is still better for larger files. But if you can't or don't want to bother with that, then this middle solution will gain you better efficiency without the hassle. Just mentioning the FileResponse object available in Django 1.
Edit: Just ran into my own answer while searching for an easy way to stream files via Django, so here is a more complete example to future me.
That's not fine of course. Adding another line from elo80ka solved the problem. Here is the code I'm using now:. Hope it helps. Thanks to elo80ka, S.
You should use sendfile apis given by popular servers like apache or nginx in production. Many years i was using sendfile api of these servers for protecting files. You can access the source code here.
UPDATE: in new version python provider uses django FileResponse if available and also adds support for many server implementations from lighthttp, caddy to hiawatha. For example,. Django recommend that you use another server to serve static media another server running on the same machine is fine. They recommend the use of such servers as lighttp. This is very simple to set up. Django Docs - Static Files. I have faced the same problem more then once and so implemented using xsendfile module and auth view decorators the django-filelibrary.
Feel free to use it as inspiration for your own solution. This module provides a simple way to serve files for download in django rest framework using Apache module Xsendfile. Open views. You can change file name and path as per your requirement. If you want to download the file via template, just add the hyperlink to the file as shown below in any template. In this case, you need to modify the views. In the above code, if the filename is empty, then Django will load file.
You can use the same method for downloading PDF files or other types of files too. Just update the hyperlink in file.
0コメント