django - How to get the list of the authenticated users? -
i display list of authenticated users.
on documentation: http://docs.djangoproject.com/en/dev/topics/auth/
class models.user
is_authenticated()¶
returns true. way tell if user has been authenticated. ...
you can know on template side current user authenticated or not:
{% if user.is_authenticated %} {% endif %}
but didn't found way list of authenticated users.
any idea?
going along rz's answer, query session
model non-expired sessions, turn session data users. once you've got turn template tag render list on given page.
(this untested, close working).
fetch logged in users...
from django.contrib.auth.models import user django.contrib.sessions.models import session django.utils import timezone def get_all_logged_in_users(): # query non-expired sessions # use timezone.now() instead of datetime.now() in latest versions of django sessions = session.objects.filter(expire_date__gte=timezone.now()) uid_list = [] # build list of user ids query session in sessions: data = session.get_decoded() uid_list.append(data.get('_auth_user_id', none)) # query logged in users based on id list return user.objects.filter(id__in=uid_list)
using this, can make simple inclusion template tag...
from django import template wherever import get_all_logged_in_users register = template.library() @register.inclusion_tag('templatetags/logged_in_user_list.html') def render_logged_in_user_list(): return { 'users': get_all_logged_in_users() }
logged_in_user_list.html
{% if users %} <ul class="user-list"> {% user in users %} <li>{{ user }}</li> {% endfor %} </ul> {% endif %}
then in main page can use like...
{% load your_library_name %} {% render_logged_in_user_list %}
edit
for talking 2-week persistent issue, i'm assuming wanting have "active users" type of listing making use of session_expire_at_browser_close
setting, though recognize isn't case.
Comments
Post a Comment