重定向中的URL片段为LoginRequiredMixin [英] URL Fragments in Redirect for LoginRequiredMixin
问题描述
我正在使用 django-braces的LoginRequiredMixin 一个Django 1.6项目。这个mixin复制了Django的login_required装饰器。
I am using django-braces's LoginRequiredMixin in a Django 1.6 project. This mixin replicates Django's login_required decorator.
我有一个视图,使用具有如下URL的 LoginRequiredMixin
/ spa_home /#price_requests / 68
。如果我尝试在不登录的情况下点击此URL,那么mixin正确地将我发送到登录页面,请求如下: / accounts / login /?next = / spa_home /#price_requests / 68
。不幸的是,在成功登录之后,URL哈希片断已被关闭,我刚被重定向到 / spa_home /
。
I have a view that uses the LoginRequiredMixin
that has a URL like this: /spa_home/#price_requests/68
. If I try to hit this URL without being logged in, the mixin correctly sends me to the login page with a request like this: /accounts/login/?next=/spa_home/#price_requests/68
. Unfortunately, after successfully logging in, the URL hash fragment is left off and I am just redirected to /spa_home/
.
解决这个问题的最好方法是什么?从我的应用程序中删除哈希片段将是一个很大的努力。
What is the best way to fix this? Removing hash fragments from my application would be a large effort.
推荐答案
问题是浏览器解释登录URL的方式。你想要这样的内容:
The issue is the way the browser interprets the login URL. You want it to be intepreted like this:
/accounts/login/?next="/spa_home/#price_requests/68"
但实际上,它看起来像这样:
but actually, it is seen like this:
"/accounts/login/?next=/spa_home/"#price_requests/68
换句话说,哈希被看作是附加到登录URL本身,而不是重定向参数。
In other words, the hash is seen as attaching to the login URL itself, not the redirect parameter.
解决这个问题的方法是引用参数:
The way to fix this is to quote the parameter:
urllib.quote('/spa_home/#price_requests/68')
这将为您提供 / spa_home /%23price_requests / 68
正确。
这篇关于重定向中的URL片段为LoginRequiredMixin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!