117
def index(request):
    the_user = request.user

In Django, how do I know if it's a real user or not? I tried:

if the_user: but "AnonymousUser" is there even if no one logs in. So, it always returns true and this doesn't work.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

8 Answers8

210

You can check if request.user.is_anonymous returns True.

phoenix
  • 7,988
  • 6
  • 39
  • 45
Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • 28
    Be aware that in `views.py` you should use `request.user.is_anonymous()` since it's a function, while in templates you should use `{{user.is_anonymous}}` – amigcamel Oct 06 '14 at 12:29
  • 14
    Seems like in Django 1.9 it is rather `is_authenticated()`: please see https://docs.djangoproject.com/en/1.9/topics/auth/default/#authentication-in-web-requests – Paolo Stefan Mar 10 '16 at 07:04
  • 17
    from Django 1.10, is_anonymous is no longer a method (just a property) – maxbellec Sep 08 '16 at 12:58
  • 2
    I must concur with Paolo Stefan, the method you want to use is `is_authenticated()`. See also http://thegarywilson.com/blog/2006/is_authenticated-vs-is_anonymous/ – Al Sweigart Oct 21 '17 at 22:38
  • 4
    The current recommendation is to use `request.user.is_authenticated`, which is an attribute in Django 1.10+, the same as `is_anonymous` is now - see https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.User.is_anonymous. Note that if you are also using Django Guardian then these attributes won't do what you think they will - see https://django-guardian.readthedocs.io/en/stable/configuration.html – rhunwicks Jan 07 '19 at 12:02
  • Django docs say to use `is_authenticated` instead of `is_anonymous`. See [my answer](https://stackoverflow.com/a/65945751/426839). – Jeff Bowen Apr 26 '22 at 01:38
20

An Alternative to

if user.is_anonymous():
    # user is anon user

is by testing to see what the id of the user object is:

if user.id == None:
    # user is anon user
else:
    # user is a real user

see https://docs.djangoproject.com/en/dev/ref/contrib/auth/#anonymous-users

leifos
  • 243
  • 2
  • 3
  • 4
    seems a bad idea. user.is_anonymous() will keep working in the new versions, user.id might not, depending on future implementations – maxbellec Sep 08 '16 at 12:57
  • 1
    Also, be aware that if you are using Django Guardian then the anonymous user will also have an id - see https://django-guardian.readthedocs.io/en/stable/configuration.html#configuration. – rhunwicks Jan 07 '19 at 11:53
  • 1
    And the current recommendation as per https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.User.is_anonymous is to use User.is_authenticated – rhunwicks Jan 07 '19 at 11:55
  • This doesn't work unfortunately. It was the first thing I tried. Seems that the "anonymous" user shows up when using user == None (and other variations). – Harlin Feb 16 '19 at 03:16
9

You should check the value of request.user.is_authenticated. It will return True for a User instance and False for an AnonymousUser instance.

One answer suggests using is_anonymous but the django.contrib.auth documentation says “you should prefer using is_authenticated to is_anonymous”.

Jeff Bowen
  • 5,904
  • 1
  • 28
  • 41
4

I know I'm doing a bit of grave digging here, but a Google search brought me to this page.

If your view def requires that the user is logged in, you can implement the @login_required decorator:

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    …
Karl M.W.
  • 728
  • 5
  • 19
4

I had a similar issue except this was on a page that the login_redirect_url was sent to. I had to put in the template:

{% if user.is_authenticated %}
    Welcome Back, {{ username }}
{% endif %}
Harlin
  • 1,059
  • 14
  • 18
2

In Django rest framework it's good idea to use permission classes to check if user is authenticated. This will apply to whole viewSet. In the simpliest form it could look like this:

from rest_framework.permissions import IsAuthenticated
...
class SomeViewSet(viewsets.GenericViewSet):
    permission_classes = [IsAuthenticated]
TCFDS
  • 584
  • 4
  • 16
0

You should use request.user.is_authenticated as it is the preferred solution over the request.user.is_anonymous (the accepted answer)

mPrinC
  • 9,147
  • 2
  • 32
  • 31
0

You can use is_authenticated and is_anonymous with HttpRequest.user and get_user(request) to check if the user is anonymous or not in Django Views as shown below. *The doc recommends is_authenticated instead of is_anonymous:

# "views.py"

from django.shortcuts import render, 
from django.contrib.auth import get_user

def test(request):
    print(request.user.is_authenticated) # True or False
    print(request.user.is_anonymous) # True or False
    print(get_user(request).is_authenticated) # True or False
    print(get_user(request).is_anonymous) # True or False
    return render(request, 'index.html')

And, you can check if the user is anonymous or not in Django Templates as shown below:

{% "index.html" %}

{{ user.is_authenticated }} {% True or False %}
{{ user.is_anonymous }} {% True or False %}
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129