重定向到管理员进行登录 [英] Redirect to admin for login

查看:102
本文介绍了重定向到管理员进行登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义为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:http://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):
   ...

这篇关于重定向到管理员进行登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆