我有一个定义为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): ...更多推荐
重定向到管理员进行登录
发布评论