3

How can I know if a specific user is logged in Django (not the currently requesting user)?

I tried this:

user = User.objects.get(username="jon")
if user.is_authenticated():
   print "user logged"

But this always returns True if the username matched.

cfedermann
  • 3,286
  • 19
  • 24
rayashi
  • 1,721
  • 3
  • 20
  • 28
  • 1
    @linker No, it's not a duplicate of that at all. That person was referencing the function but not calling it. rayashi is clearly calling the function correctly. – agf Apr 14 '12 at 20:13
  • 1
    To whoever downvoted -- I think this is a fine question (I'm out of votes). He showed what he tried, and told us why that wasn't working for him. – agf Apr 14 '12 at 20:18
  • 1
    What do you class as logged in? Most of the time if you want to know this, you want to know if they are active - not just if they haven't logged out since the last time they logged in. If so, how long since they last viewed a page counts as active? – Gareth Latty Apr 14 '12 at 20:20
  • Yes it is correct ... but every user I found the function return True .. – rayashi Apr 14 '12 at 20:21
  • This is intended behavior: [see docs](https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.is_authenticated) The is_authenticated() method only tells you that the user has a valid username and password (which is guaranteed). – chandsie Apr 14 '12 at 20:22
  • 1
    Also you can look at this question, it's somewhat related: http://stackoverflow.com/questions/2723052/how-to-get-the-list-of-the-authenticated-users – chandsie Apr 14 '12 at 20:29
  • @chands Good Link! Querying the session model does seem to be the way to go. – agf Apr 14 '12 at 20:31
  • @Lattyware I would like know if the user do login and not yet do logout... sorry – rayashi Apr 14 '12 at 20:33

2 Answers2

4

The short answer is you can't; at least not with anything built-in to Django. To know if any user in the database is logged in you essentially are asking if the user is tied to an active session. But you cannot query the session table (short of querying all active sessions) for the user id since this information is stored as pickled data. And even if you did this is not entirely meaningful depending on how long the session cookie lasts (default 2 weeks).

So what can you do? One thing you can query about the user is the last time they logged in. For instance you could get user which last logged in less than 10 minutes ago:

from datetime import datetime, timedelta
from django.contrib.auth.models import User

cutoff = datetime.now() - timedelta(minutes=10)
active = User.objects.filter(last_login__gt=cutoff)

Another thing you can do is track this on your own. In fact there is an app which does just that called django-tracking.

Mark Lavin
  • 24,664
  • 5
  • 76
  • 70
1

No in-built method exists to do what you're looking for, however there is an app that you can plugin to your project that lets you do what you want. It's called django-tracking and you can find it here: https://github.com/codekoala/django-tracking

EDIT Whoops! I got beat to it, but basically what Mark Lavin said.

chandsie
  • 2,865
  • 3
  • 27
  • 37
  • Sorry.. I cant import "from tracking.models import Visitor" it return "Unresolved import: Visitor Unused import: Visitor" – rayashi Apr 16 '12 at 19:47
  • Make sure python can find where you've stored the tracking app. If you're using eclipse this should be a simple matter of adding the location to the projects referenced libraries. If not then you'll have to manually edit your `PYTHONPATH` system variable. – chandsie Apr 17 '12 at 01:15