In django, you can know all logged in users by Session
for non-expired sessions.
For that, just make get_all_logged_in_users
. (maybe in function.py
or somewhere else.)
# If you use custom user, import it instead of User
from django.contrib.auth.models import User
from django.contrib.sessions.models import Session
from django.utils import timezone
def get_all_logged_in_users():
sessions = Session.objects.filter(expire_date__gte=timezone.now())
uid_list = []
for session in sessions:
data = session.get_decoded()
uid_list.append(data.get('_auth_user_id', None))
return User.objects.filter(id__in=uid_list)
In views.py
, you can call get_all_logged_in_users
and send it by context
from django.shortcuts import render
from django.contrib.auth.models import User
def chat_room(request):
...
users = User.objects.all()
logged_users = get_all_logged_in_users()
...
return render(request, 'chat/room.html', {
'users': users,
'logged_users': logged_users,
})
Then you can use logged_users
in your template.
<ul class="user-list">
{% for user in users %}
{% if user in logged_users %}
<li class="exist">{{ user }}</li>
{% else %}
<li>{{ user }}</li>
{% endif %}
{% endfor %}
</ul>
'backend > django' 카테고리의 다른 글
Using Environment variable in uswgi.ini (0) | 2018.04.22 |
---|---|
django-redis install and basic usage (0) | 2018.04.16 |
Django: django channels basic usage (0) | 2018.04.06 |
Django: template tag 'now' (0) | 2018.04.05 |
Django: use crontab easy in django - django-crontab (0) | 2018.04.04 |