Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get django to return both an HttpResponse AND render a different template

Tags:

django

i have a method in views.py that can download a file i've constructed via an HttpResponse['Content-Disposition'] attachment:

response = HttpResponse(content_type='text/json')
response['Content-Disposition'] = 'attachment; filename="%s"' % (dgjf)
response.write(dgjson)

this works fine if my the method returns response

but, after the download i want to redirect to a confirmation page that i render like this:

confirmMsg = render(request,'confirmation.html',context)

which i would normally return from the same method.

but how can i get the response to do download AND also redirect to the confirmation?

like image 715
rikb Avatar asked Apr 28 '17 19:04

rikb


People also ask

What is the difference between render and HttpResponse in Django?

Abid covers it, render is usually used to load a template and a context, while HttpResponse is usually for data. As it's bad practice to "respond" with html. Render is essentially a shortuct for HttpResponse , It provides a more effective way to modify templates, and load data dynamically.

Which response object allows decorators or middleware to modify a response after it has been constructed by the view?

However, it can sometimes be beneficial to allow decorators or middleware to modify a response after it has been constructed by the view. For example, you may want to change the template that is used, or put additional data into the context. TemplateResponse provides a way to do just that.

What is HttpResponse in Django?

HttpResponse (source code) provides an inbound HTTP request to a Django web application with a text response. This class is most frequently used as a return object from a Django view.


1 Answers

You need to redirect to the after/confirmation page first, and then force the download. Your after-page would say something like "Your download should start automatically, if not click.. etc.".

From the after page you initiate the download either through Javascript, http-refresh, or an iframe.

Javascript, in your after-page template insert:

<script>location.href="download/url/here";</script>

http-refresh, in your after-page view, annotate the HttpResponse before returning it:

response = render_to_response(...)
response['Refresh'] = "0;url=/download/url/here"
return response

..or add the following tag to the header of your after-page (the 0 before url is the number of seconds to wait before redirecting):

<meta http-equiv="Refresh" content="0;url=/download/url/here">

an iframe would simply be (you'll probably need to remove borders etc. too):

<iframe src="/download/url/here" style="width:0;height:0"></iframe>

The idea behind all of these is that a download doesn't change the pixels on the screen, so initializing the download once you have the after-page on-screen will look like the page is downloading the file (and not look like it is redirecting - which is what it's really doing).

like image 196
thebjorn Avatar answered Oct 02 '22 17:10

thebjorn