4

I know it's fairly simple to add a redirect to Django with HttpResponseRedirect, but is it possible to make a delayed redirect?

What I want to add is like a confirmation page for the user that will basically say that "Your request has been successful" for 3 seconds and then be taken back to the login page or something.

I've read about JQuery, but as far as I know that's for JavaScript and I want to keep this as Python as possible.

Any ideas?

vvns
  • 3,548
  • 3
  • 41
  • 57
user2090916
  • 91
  • 2
  • 6
  • I think you'll have to use javascript for it. For example, your django view returns a flag "redirect" in the context of a normal HttpResponse. Then, if flag exists, js counts to 3 and [redirects](http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript). – alecxe Sep 10 '13 at 09:40
  • 2
    it's simple, on the page "successful" you add a meta html refresh with 3sec ; no javascript needed –  Sep 10 '13 at 09:41
  • 2
    I'm afraid this is not how HTTP is designed to work. Timing is an aspect of state which is not a feature of HTTP. Once a request is responded, the server is done. It doesn't keep tab of how long the request has been issued and issue another unless the client explicitly asked for it. – Lim H. Sep 10 '13 at 09:42

1 Answers1

11

You can use a templateView to display your "successful" page then in the template add

<meta http-equiv="refresh" content="3;url=http://foobar.somewhereelse.com/"> 
  • Important! You need to place that inside block of content or this woldn't be worked: `{% extends "base.html" %}{% block content %}

    Success! Thank you for your message

    After 5 seconds you will be redirected to the main page

    {% endblock %}`
    – Dmitriy Kisil May 24 '19 at 12:21
  • Does the URL need to be a full rather than relative one? It seemed to in my case, and this feels consistent with how HTTP itself behaves with the Location: redirect header. – Josiah Yoder Jan 30 '23 at 18:58
  • A fuller discussion of HTTP redirects can be found [here](https://stackoverflow.com/a/5411567/1048186). – Josiah Yoder Jan 31 '23 at 14:02