Pedro Assunção

Django: Reverse HTTP redirect with parameters

Here’s how to, from a view, redirect to another URL passing parameters to it. For instance, to redirect the user to a certain page after login:

return HttpResponseRedirect(reverse('dz_details', kwargs={'dz_id':dz.id}))

This will lookup the ‘dz_details’ name in your urls.py file, which could be defined like so:

url(r'^details/(\d+)/$', views.dz_details, name='dz_details'),

There is a simpler way to do it, if you don’t want to use the parameters’ names, but you have to know the order on which they appear in the method:

return HttpResponseRedirect(reverse('dz_details', args=[ dz.id ]))

Related:

  1. [Django] Dynamic redirection after login Here’s how to redirect to the login page in django,...
  2. Java HTTP proxy servlet (with Spring) I always wanted to build my own proxy. I’m kidding....


Categorised as: code snipplets, computers, software development, tips


One Comment

  1. [...] This post was Twitted by nocivus [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>