xyz
xyz

Reputation: 197

how to do 2 continuous redirect in django

in my view I'm doing some calculation and then based on that result I'll show a new page where I can show success message and then automatically in 3 seconds it will redirect to another url.

def home(request):
   #do some calculation
   return HttpResponse('success') # and after 3 seconds it will redirect to new page.
   # here I  dont know how to proceed further 

OR

def home(request):
   #do some calculation
   return render(request, template) # and after 3 seconds it will redirect to new page.
   # here I  dont know how to proceed further 

Upvotes: 1

Views: 1315

Answers (2)

AlexMeng
AlexMeng

Reputation: 833

You cannot accomplish this in Django. Once the page has loaded, you'll need to use JavaScript to redirect the user. So in your template, you would need something like this:

<script>
  window.setTimeout(function(){
    window.location.href = "redirect_url";
  }, 3000);
</script>

Upvotes: 2

user4396006
user4396006

Reputation:

This has to be with HTML. If you say "after 3 seconds", just write the following code in your template and it'll redirect you to wherever you want:

<script>
// redirect to google after 3 seconds
window.setTimeout(function() {
    window.location.href = 'http://www.google.com';
}, 3000);
</script>

By the way, I have noticed you do not really get the usage of Django rendering here. If you return plain text with HttpResponse you can't do anything from client side. Rendering a template is the only way unless you code it all in the HttpResponse part which I do not really recommend.

You cannot redirect a user from the server side. You can just return a certain hypertext fragment which will make the browser understand it's got to redirect, which is what I have just written up there.

Upvotes: 2

Related Questions