How to auto refresh/redirect a view in Django
15,844
You can achieve that using Javascript, example:
window.setTimeout(function () {
location.href = "http://myurl.com";
}, 5000); // refresh/redirect after 5 seconds.
Comments
-
Hassan Baig almost 2 years
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 about 8 yearsThanks 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.
-
Ali about 8 yearsI 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 about 8 years@HassanBaig yes you can, just add them inside
<script>
. If you want the server to determine what the redirect url is, pass it to your template and thenlocation.href = "{{ redirect_url }}";
-
Hassan Baig about 8 yearsTo confirm, This should go in the <head> of the template, right?
-
Ali about 8 years@HassanBaig it works wherever you put it.
-
Shahriar.M over 3 years@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?