3

Is there a way to auto redirect (or refresh) a template (or better: it's underlying view) after the passage of a set amount of time in Django? E.g. via using Redirect view or something similar? An example would be nice.

Essentially I need to approximate <meta http-equiv="refresh" content="60;url here">, but in Django. Of course, I know exactly how the meta refresh tag in HTML works - but this question doesn't pertain to that (or to using JS to achieve the same effect). That tag has its own problems, e.g. it doesn't work in Opera Mini (a critical browser for my audience).

Hassan Baig
  • 15,055
  • 27
  • 102
  • 205

1 Answers1

12

You can achieve that using Javascript, example:

window.setTimeout(function () {
    location.href = "http://myurl.com";
}, 5000); // refresh/redirect after 5 seconds.
Ali
  • 3,568
  • 2
  • 24
  • 31
  • Thanks for the suggestion Ali. I'd consider this as "Plan B", since doing it natively inside Django remains my top priority for reasons beyond the scope of this question. – Hassan Baig Mar 13 '16 at 20:49
  • I don't believe you can refresh a page that is sent to the client using your backend; once a page is sent to the client you have no control over it. – Ali Mar 13 '16 at 20:52
  • 3
    @HassanBaig yes you can, just add them inside ` – Ali Mar 13 '16 at 22:05
  • To confirm, This should go in the of the template, right? – Hassan Baig Mar 13 '16 at 23:27
  • @HassanBaig it works wherever you put it. – Ali Mar 14 '16 at 10:59
  • @Ali what exacty should I pass to template in views.py? I mean the url name as a string? ` return render(request, context={ 'redirect_url': 'some_url_name_as_string')` is it right? – Shahriar.M Dec 15 '20 at 10:34