我想在我的 View 中使用redirect_to_login
。由于某种原因,我被重定向到登录两次(因此 URL 格式错误)。
我的TestView(View)
看起来像这样(相关部分):
def dispatch(self, request, *args, **kwargs):
if not self.test_func():
if self.redirect_to_login:
return redirect_to_login(self.request.get_full_path())
else:
return HttpResponseForbidden()
我的urls.py
(相关部分):
url(r'^test/$', TestView.as_view()),
当我在浏览器中输入localhost:8000/test/
时,我最终会看到
http://localhost:8000/login/?next=/%3Fnext%3D/test/
与预期不同
http://localhost:8000/login/?next=/test/
有什么想法可能是什么问题吗?如果您需要更多文件,请询问。
最佳答案
这个 /%3Fnext%3D/test/
等于 /?next=/test/
你只需要传递 test
这个词>。返回的内容应该是这样的:
return redirect_to_login('/test/')
有关redirect_to_login
的更多信息 https://docs.djangoproject.com/en/1.11/topics/auth/default/#helper-functions
关于python - Django - 使用redirect_to_login,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46778655/