简体   繁体   English

Django:登录后重定向到上一页*使用查询字符串*

[英]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.我正在使用django.contrib.auth并希望在登录后重定向到上一页。我想要类似以下内容: Django:登录后重定向到上一页,除了重定向到 ZE6B391A8D2C4B45902DZ 可以包含查询字符串6D45902D238A

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. user_login是我的登录视图的名称。

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 (eg /login/?next=/my/original/path/?with=other&fun=query&string=parameters ) which doesn't work.我想使用{{ request.get_full_path }}而不是{{ request.path }}来获取包含查询字符串的当前路径,但这会创建一个 url 在查询字符串中带有查询字符串(例如/login/?next=/my/original/path/?with=other&fun=query&string=parameters )这不起作用。

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.我还尝试在我的登录视图中添加一个redirect_to参数,并将带有查询字符串的 url 作为参数传递给url模板标签。 However this gives me a NoReverseMatch error.但是,这给了我一个NoReverseMatch错误。

How about escaping the get parameters and then unquoting them in the view? escaping 获取参数然后在视图中取消引用它们怎么样?

html 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: ) PS:我多次偶然发现您的博客,寻找 PIP 帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM