user object's is_authenticated() method ALWAYS return True even after they log out??

192 views
Skip to first unread message

Chris Seberino

unread,
Jul 29, 2014, 11:08:00 AM7/29/14
to django...@googlegroups.com
I tried viewing the value of is_authenticated() for a user and it always is true...before and after logging in and after signing out.

I'm trying to determine if a user has logged in or not.  Is this the right variable to use?  Why not changing?

Thanks.


cs

Andreas Kuhne

unread,
Jul 29, 2014, 11:15:43 AM7/29/14
to django...@googlegroups.com
Hi Chris,

is_authenticated() on a User should always be true. The reason for this is that you check the function on the request.user object. When a user is logged in, it will be true, however, the AnonymousUser returns false, which is the default when a user has not logged in.

So you can only check it in a view.

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a1ac670d-1406-452b-8868-aefd70d3e7cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cmawe...@gmail.com

unread,
Jul 31, 2014, 12:05:29 PM7/31/14
to django...@googlegroups.com
Right, the question isn't "is user x logged into the website right now", it actually only makes sense for for request.user. This will return all users in the database: logged_in_users = [user for user in User.objects.all() if user.is_authenticated()]

Chris Seberino

unread,
Jul 31, 2014, 2:25:24 PM7/31/14
to django...@googlegroups.com
Is there a way to answer your question "is user x logged into the website right now" in Django code?

cmawe...@gmail.com

unread,
Jul 31, 2014, 2:43:58 PM7/31/14
to django...@googlegroups.com
something like this may work:

from django.contrib.auth.models import User
from django.contrib.sessions.models import Session

User.objects.filter(id__in=(s.get_decoded().get('_auth_user_id') for s in Session.objects.all()))







for s in Session.objects.all()
   

cmawe...@gmail.com

unread,
Jul 31, 2014, 2:46:55 PM7/31/14
to django...@googlegroups.com
or for a specific user:

from django.contrib.sessions.models import Session

any(s.get_decoded().get('_auth_user_id') == user.id for s in Session.objects.all())


Reply all
Reply to author
Forward
0 new messages