Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is_authenticated returns True for AnonymousUser

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?

like image 472
Dan Abramov Avatar asked Nov 27 '22 06:11

Dan Abramov


1 Answers

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.

like image 86
Daniel Roseman Avatar answered Dec 21 '22 20:12

Daniel Roseman