python - Django - 使用redirect_to_login

标签 python django url authentication

我想在我的 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/

相关文章:

html - 如何在wicket中添加表格背景图片

python - 如何读取已执行的 shell 命令的输出?

python - 如何为Django Rest Framework指定过滤数据参数?

python - 更新中间文件时防止规则重新运行

django - 生产中的 500 Internal Server Error - Django

python - E : Unable to find a source package for psycopg2 or use pip with virtualenv

python - Python 3 str.join() 是否保证顺序?

python - django 脆皮表单按钮不显示

javascript - 如何使用 window.open() 快速访问我网站的根目录?

ruby - 用真实单词生成随机 URL