Django download file response






















Show 3 more comments. Lott S. Lott k 75 75 gold badges silver badges bronze badges. Good point about saving memory. But if using a temporary file, where would you put the code to delete it? Django already has an admin command that must be run periodically to remove old sessions.

Lott Is it possible to serve the created file z in in your example using mod x-sendfile? Why not make a tar file instead? Guillaume Lebreton 1, 13 13 silver badges 23 23 bronze badges. Roshan Roshan 1 1 silver badge 2 2 bronze badges. Soviut Soviut Community Bot 1 1 1 silver badge. Ryan Anguiano Ryan Anguiano 61 1 1 silver badge 1 1 bronze badge. Looks unsafe to create in-memory string of image size. Kamesh Jungi Kamesh Jungi 5, 5 5 gold badges 36 36 silver badges 49 49 bronze badges.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Note: HttpResponse uses the iterator object directly, stores the contents of the iterator object in a city string, returns it to the client, and frees up memory.

This can be seen as a time-consuming and memory-consuming process when the file gets larger. Streaming HttpResponse streams file content, which can be used for large amounts of data.

Three types of http response objects are described on the django website. FileResponse is recommended, as you can see from the source that FileResponse is a subclass of StreamingHttpResponse and uses iterators internally for data streaming.

Programmer Group A programming skills sharing group. Writing in models. Connect and share knowledge within a single location that is structured and easy to search. I tried the following:. Here's an example of returning a file download directly from DRF. The trick is to use a custom renderer so you can return a Response directly from the view:.

Note I'm using a custom endpoint download instead of the default endpoint retrieve , because that makes it easy to override the renderer just for this endpoint instead of for the whole viewset -- and it tends to make sense for list and detail to return regular JSON anyway. If you wanted to selectively return a file download you could add more logic to the custom renderer. For FrontEnd code refer this.

For me, using Python 3. 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? 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?



0コメント

  • 1000 / 1000