重定向到管理员进行登录

编程入门 行业动态 更新时间:2024-10-26 16:25:07
本文介绍了重定向到管理员进行登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个定义为url'site / main /'的视图。我想要(未经身份验证)的用户重定向到默认的/ admin /页面进行登录,然后在成功登录后重定向到/ main /页面。我遵循django文档,但是我必须丢失一些东西,因为我无法让它工作。 我的视图看起来像:

I have a view defined for a url 'site/main/'. I would like to be able to have (unauthenticated) users redirected to the default '/admin/' page for login, then redirected to the '/main/' page after successful login. I followed the django documentation, but I must be missing something as I am unable to get this to work. My view looks like:

def main(request): if not request.user.is_authenticated(): return HttpResponseRedirect('admin/?next=%s' % request.path) else:

我收到错误:

未找到页面(404) 请求方法:GET 请求URL :http:// sitename:8080 / main / admin /?next = / main /

Page not found (404) Request Method: GET Request URL:sitename:8080/main/admin/?next=/main/

任何帮助都非常感谢!

推荐答案

你缺少一个初始的 / 在URL中: / admin /?next = ...

You're missing an initial / in the URL: /admin/?next=...

然而,这仍然不起作用,因为 admin URL不知道下一个参数的任何内容。这只适用于实际的登录视图。使用您的代码,用户将被登录到管理员,但不会重定向到您的页面。

However this still won't work, as the admin URL doesn't know anything about the next parameter. That's only for the actual login views. With your code, the user will be logged into the admin but will not be redirected back to your page.

您应该建立一个登录模板并将其连接到内置 - 登录视图。然后,不要在视图中检查 is_authenticated ,您应该使用 login_required 装饰器。

You should build a login template and wire it up to the built-in login views. Then instead of checking is_authenticated in the view, you should just use the login_required decorator.

@login_required def main(request): ...

更多推荐

重定向到管理员进行登录

本文发布于:2023-10-31 18:32:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   管理员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!