I am struggling with is_authenticated
returning True
when I'm not logged in:
u = request.user
if u.is_authenticated:
raise Exception('I am said to be authenticated, but I really am not.')
To clarify, Django debug view correctly identifies u
as an AnonymousUser
:
u <django.contrib.auth.models.AnonymousUser object at 0x9e76f4cc>
Even more odd, inside the template is_anonymous
works fine:
{% if not request.user.is_authenticated %}
We are anonymous.
{% endif %}
Why is that?
It's a method, not a property. You need to call it:
if u.is_authenticated():
Of course, in a template, Django calls methods for you automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With