How to make django return HttpResponse and render a different template
I have a method in views.py
which can load the file I have built via 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 method returns response
but after uploading i want to redirect to confirmation page which i execute like this:
confirmMsg = render(request,'confirmation.html',context)
which I usually return from the same method.
but how can I get the response to download AND also redirect to confirmation?
source to share
First you need to redirect to the page after / confirm and then force the download. Your postprogram will say something like "Your download should start automatically unless you click .. etc.".
On the next page, you trigger the download via Javascript, http-refresh, or iframe.
Javascript in your template after the page:
<script>location.href="download/url/here";</script>
http-refresh, in your aftermarket 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 your page header (0 before the URL is the number of seconds to wait before redirecting):
<meta http-equiv="Refresh" content="0;url=/download/url/here">
the iframe will be simple (you will probably need to remove borders as well, etc.):
<iframe src="/download/url/here" style="width:0;height:0"></iframe>
The idea behind all this is that loading does not change the pixels on the screen, so initializing the loading after you close the screen on the screen would look like this: the page is loading the file (rather than looking like it's a redirect - which he really does).
source to share