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.
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.
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/…
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.
You can check if request.user.is_anonymous
returns True
.
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
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