Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check whether this user is anonymous or actually a user on my system?

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.

like image 562
TIMEX Avatar asked Jan 09 '11 23:01

TIMEX


People also ask

When a user is anonymous?

What is an Anonymous User? Anonymous User is any user who accesses network resources without providing a username or password. Some Microsoft Windows Server applications like Microsoft Internet Information Services (IIS) can be configured to allow anonymous users to access their resources.

How do I check if a user is anonymous on Django?

You can check if request. user. is_anonymous returns True . Seems like in Django 1.9 it is rather is_authenticated() : please see docs.djangoproject.com/en/1.9/topics/auth/default/…

How does Django authentication work?

The Django authentication system handles both authentication and authorization. Briefly, authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do. Here the term authentication is used to refer to both tasks.


2 Answers

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

like image 194
Daniel DiPaolo Avatar answered Sep 27 '22 19:09

Daniel DiPaolo


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

like image 25
leifos Avatar answered Sep 27 '22 17:09

leifos