我有一个为 url 'site/main/' 定义的 View 。我希望能够将(未经身份验证的)用户重定向到默认的“/admin/”页面进行登录,然后在成功登录后重定向到“/main/”页面。我遵循了 django 文档,但我一定遗漏了一些东西,因为我无法让它工作。 我的观点如下:
def main(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('admin/?next=%s' % request.path)
else:
我得到一个错误:
找不到页面 (404)
请求方式:GET
请求网址:http://sitename:8080/main/admin/?next=/main/
非常感谢任何帮助!
最佳答案
您在 URL 中缺少初始 /
:/admin/?next=...
但是这仍然不起作用,因为 admin
URL 对 next
参数一无所知。这仅适用于实际登录 View 。使用您的代码,用户将登录到管理员,但不会被重定向回您的页面。
您应该构建一个登录模板并将其连接到内置登录 View 。然后不要在 View 中检查 is_authenticated
,您应该只使用 login_required
装饰器。
@login_required
def main(request):
...
https://stackoverflow.com/questions/11122293/