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

sal*_*ane 11 python django django-authentication

我正在使用django.contrib.auth并希望在登录后重定向到上一页.我想要类似以下内容:Django:登录后重定向到上一页, 重定向到的URL可以包含查询字符串.

目前我的模板中有以下链接:

<a href="{% url user_login %}?next={{ request.get_full_path }}">Login</a>
Run Code Online (Sandbox Code Playgroud)

user_login 是我的登录视图的名称.

我想使用{{ request.get_full_path }}而不是{{ request.path }}获取当前路径,包括查询字符串,但这将创建一个带有查询字符串(例如/login/?next=/my/original/path/?with=other&fun=query&string=parameters)中的查询字符串的URL,但不起作用.

我还尝试redirect_to在我的登录视图中添加一个参数,并将带有查询字符串的url作为arument传递给url模板标记.但是这给了我一个NoReverseMatch错误.

Yuj*_*ita 11

如何转义获取参数然后在视图中取消引用它们?

HTML

<a href="{% url user_login %}?next={{ request.get_full_path|urlencode }}">Login</a>
Run Code Online (Sandbox Code Playgroud)

登录视图

if successful_login:
    url_with_get = urllib2.unquote(request.GET.get('next'))
    return http.HttpResponseRedirect(url_with_get)
Run Code Online (Sandbox Code Playgroud)

PS:我多次偶然发现你的博客寻找PIP帮助:)