How to check whether a user is online in django template?

As the documentation says:

Even though normally you will check is_autheticated attribute on request.user to find out whether it has been populated by the AuthenticationMiddleware (representing the currently logged-in user), you should know this attribute is True for any User instance.

So to check if user is online I would do something like this:

class Profile(models.Model):
    user = models.OneToOneField(User, related_name='profile')           
    is_online = models.BooleanField(default=False)

from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.dispatch import receiver    

def got_online(sender, user, request, **kwargs):    
    user.profile.is_online = True

def got_offline(sender, user, request, **kwargs):   
    user.profile.is_online = False

And then in your template you would check if user is online:

{% if user.profile.is_online %}
{% else %}
{% endif %}

Don't forget to return user instance to your template in order for user.profile.is_online to work.

first install django-online-users by running

pip install django-online-users

then in your


Then in your views

import online_users.models

def see_users(self):

  user_status = online_users.models.OnlineUserActivity.get_user_activities(timedelta(seconds=60))
  users = (user for user in  user_status)
  context = {"online_users"}`

Pass the context to your template for example

{% for user in users %}
{% endfor %}

This will output the users who have been active and online for the last 60 seconds

‌Thanks this excellent blog post, with slight modifications, I came up with a better solution, which uses memcache, hence less delay per request:

in add:

from django.core.cache import cache 
import datetime
from myproject import settings

and add these method the userprofile class:

def last_seen(self):
    return cache.get('seen_%s' % self.user.username)

def online(self):
    if self.last_seen():
        now =
        if now > self.last_seen() + datetime.timedelta(
            return False
            return True
        return False 

in userprofile folder add this

import datetime
from django.core.cache import cache
from django.conf import settings

class ActiveUserMiddleware:

    def process_request(self, request):
        current_user = request.user
        if request.user.is_authenticated():
            now =
            cache.set('seen_%s' % (current_user.username), now, 

in add 'userprofile.middleware.ActiveUserMiddleware', to MIDDLEWARE_CLASSES and also add:

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
            'LOCATION': '',              

# Number of seconds of inactivity before a user is marked offline

# Number of seconds that we will keep track of inactive users for before 
# their last seen is removed from the cache
USER_LASTSEEN_TIMEOUT = 60 * 60 * 24 * 7

and in profile.html:

   <tr><th>Last Seen</th><td>{% if profile.last_seen %}{{ profile.last_seen|timesince }}{% else %}awhile{% endif %} ago</td></tr>
   <tr><th>Online</th><td>{{ }}</td></tr>

That's it!

To test cache in the console, to make sure that memcache works fine:

$memcached -vv
$ python shell
>>> from django.core.cache import cache
>>> cache.set("foo", "bar")
>>> cache.get("foo")
>>> cache.set("foo", "zaq")
>>> cache.get("foo")

You can have an integer field for each user saying the number of sessions the user has logged in currently. You can increase that by 1 every time the user logs in somewhere, and reduce it by 1 when the user logs out somewhere.

{% if topic.creator.login_count %}
{% else %}
{% endif %}

This is a simple way to solve this problem. You can refresh this data through an ajax request periodically.