Skip to content
Advertisement

How to check if a user is logged in (how to properly use user.is_authenticated)?

I am looking over this website but just can’t seem to figure out how to do this as it’s not working. I need to check if the current site user is logged in (authenticated), and am trying:

request.user.is_authenticated

despite being sure that the user is logged in, it returns just:

>

I’m able to do other requests (from the first section in the url above), such as:

request.user.is_active

which returns a successful response.

Advertisement

Answer

Update for Django 1.10+

is_authenticated is now an attribute in Django 1.10.

if request.user.is_authenticated:
    # do something if the user is authenticated

NB: The method was removed in Django 2.0.

For Django 1.9 and older

is_authenticated is a function. You should call it like

if request.user.is_authenticated():
    # do something if the user is authenticated

As Peter Rowell pointed out, what may be tripping you up is that in the default Django template language, you don’t tack on parenthesis to call functions. So you may have seen something like this in template code:

{% if user.is_authenticated %}

However, in Python code, it is indeed a method in the User class.

Advertisement