Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Redirect to previous page *with query string* after login

I am using django.contrib.auth and would like to redirect to the previous page after logging in. I would like something like the following: Django: Redirect to previous page after login except the redirected-to URL can contain a query string.

Currently I have the following link in my template:

<a href="{% url user_login %}?next={{ request.get_full_path }}">Login</a>

user_login is the name of my login view.

I would like to use {{ request.get_full_path }} instead of {{ request.path }} to get the current path including the query string, but this would create a url with a query string within a query string (e.g. /login/?next=/my/original/path/?with=other&fun=query&string=parameters) which doesn't work.

I also tried adding a redirect_to argument to my login view and passing the url with the query string as a arument to the url template tag. However this gives me a NoReverseMatch error.

like image 384
saltycrane Avatar asked Jun 29 '11 02:06

saltycrane


1 Answers

How about escaping the get parameters and then unquoting them in the view?

html

<a href="{% url user_login %}?next={{ request.get_full_path|urlencode }}">Login</a>

login view

if successful_login:
    url_with_get = urllib2.unquote(request.GET.get('next'))
    return http.HttpResponseRedirect(url_with_get)

PS: I've stumbled across your blog many times looking for PIP help : )

like image 198
Yuji 'Tomita' Tomita Avatar answered Sep 27 '22 02:09

Yuji 'Tomita' Tomita