Django:注销后 is_authenticated 和 is_anonymous 均返回 true
我正在使用 django-registration,然后进行设置。
{{user.is_authenticated }}
是的,即使我已经转到 /accounts/logout/ 并注销用户。
{{user.is_anonymous }}
也返回 true。根据 django 文档,这两个应该是不同的:
is_anonymous:始终返回 False。这是区分 User 和 AnonymousUser 对象的一种方法。一般来说,您应该更喜欢使用 is_authenticated() 而不是此方法。
和
is_authenticated:始终返回 True。这是一种判断用户是否已通过身份验证的方法。这并不意味着任何权限,也不检查用户是否处于活动状态 - 它仅表明用户提供了有效的用户名和密码。
我正在使用 django-registration 附带的标准视图,但尚未触及它们。在模板中我有以下代码:
{% if user.is_authenticated %}
{% user }}
{% if user.is_anonymous %}
is anonymous
{% endif $}
{% else %}
gotta login
{% endif %}
问题出在哪里?我将非常感激!
更新: 我注意到在主页上,is_authenticated 和 id_anonymous 都返回 True,而如果我在登录之前转到 /accounts/login,则只有 is_anonymous 返回 true,因为它应该是。另外,在主页上,我有以下视图(如果有帮助的话):
def home(request):
return render_jinja(request, 'index.html', blah = 'ga')
更新2: print(request.user.is_authenticated()) 给出 False。然后,我有:
return render_jinja(request, 'index.html', blah = 'ga')
在模板中, user.is_authenticated 返回 FALSE 。
更新3: 如果我使用 render_to_response,而不是 render_jinja,一切都很好。但仍然不知道如何解决这个问题:(
I am using django-registration, and just set it up.
{{user.is_authenticated }}
is true, even though i went already to /accounts/logout/ and logged the user out.
{{user.is_anonymous }}
returns true also. According to django docs, those two should be different:
is_anonymous: Always returns False. This is a way of differentiating User and AnonymousUser objects. Generally, you should prefer using is_authenticated() to this method.
and
is_authenticated: Always returns True. This is a way to tell if the user has been authenticated. This does not imply any permissions, and doesn't check if the user is active - it only indicates that the user has provided a valid username and password.
I am using the standard views that come with django-registration and haven't touched them yet. In the tempalate i have the following code:
{% if user.is_authenticated %}
{% user }}
{% if user.is_anonymous %}
is anonymous
{% endif $}
{% else %}
gotta login
{% endif %}
Where would the problem be? I will be really thankful!
UPDATE:
i have noticed that on the homepage, it both is_authenticated and id_anonymous return True, while if i go to /accounts/login before loging in, only is_anonymous returns true as it should be. And also, on the homepage, i have the following view if that helps:
def home(request):
return render_jinja(request, 'index.html', blah = 'ga')
UPDATE 2:
print(request.user.is_authenticated()) gives False. Then, i have:
return render_jinja(request, 'index.html', blah = 'ga')
and in the template, user.is_authenticated returns FALSE.
UPDATE 3:
If i use render_to_response, instead of render_jinja, all is good. still don't know how to fix this though :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来你正试图同时弄清楚两件事:使用 jinja 模板的正确方法是什么以及 User/AnonymousUser 的处理方式是什么。也许尝试一次解决一个问题。
我对 jinja 没有经验,但你可能需要检查并确保你了解 jinja 和 django 之间的差异。考虑到模板语法。我知道的最大区别是方法调用需要显式括号。因此,示例中的 is_authenticated 和 is_anonymous 调用需要括号。
如果这不能解决问题,请尝试安装 django-debug-toolbar 并查看在您的模板的上下文中。检查并查看
user
是否为 None 或对象(User 或 AnonymousUser)。您还可以阅读 AnonymousUser 并查看以下示例检查文档中经过身份验证的用户 。简而言之,对于 AnonymousUser 对象,is_anonymous() 始终返回 True 而不是 False,is_authenticated() 始终返回 False 而不是 True。
It seems like you are trying to figure out two things at once; what is the correct way to use jinja templates and what is the deal with User/AnonymousUser. Maybe try to figure out these problems one at a time.
I have no experience with jinja but you may want to check and make sure that you are taking differences between jinja and django template syntax into account. The biggest difference I know of is that a method call requires explicit parenthesis. So the is_authenticated and is_anonymous calls in your example need parenthesis.
If that does not solve the problem, try installing django-debug-toolbar and take a look at the context for your template. Check and see if
user
is None or an object (User or AnonymousUser).You can also read up on AnonymousUser and see an example of checking for an authenticated user in the docs. In a nutshell, for an AnonymousUser object is_anonymous() always returns True instead of False and is_authenticated() always returns False instead of True.
拍在头上。我在某处读到:
因此,模板中不应包含 {{user.is_authenticated}},而应该是 {{user.is_authenticated()}}
Smack on the head. I read somewhere:
And so, instead of having {{user.is_authenticated}} in template, it should be {{user.is_authenticated()}}