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

197 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Chris Seberino

ungelesen,
29.07.2014, 11:08:0029.07.14
an 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

ungelesen,
29.07.2014, 11:15:4329.07.14
an 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

ungelesen,
31.07.2014, 12:05:2931.07.14
an 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

ungelesen,
31.07.2014, 14:25:2431.07.14
an 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

ungelesen,
31.07.2014, 14:43:5831.07.14
an 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

ungelesen,
31.07.2014, 14:46:5531.07.14
an 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())


Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten