Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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).

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!