如何在django模板中检查用户是否在线?
How to check whether a user is online in django template?
在模板中,当我使用
{% if topic.creator.is_authenticated %}
Online
{% else %}
Offline
{% endif %}
用户总是在线,即使他们刚刚退出。所以我想知道如何正确查看在线用户数?
您可以为每个用户设置一个整数字段,说明该用户当前登录的会话数。您可以在用户每次在某处登录时将其增加 1,并在用户在某处注销时将其减少 1。
{% if topic.creator.login_count %}
Online
{% else %}
Offline
{% endif %}
这是解决这个问题的简单方法。您可以定期通过 ajax 请求刷新此数据。
中描述了用户似乎始终在线的原因
is_authenticated()
- Always returns
True
... This is a way to tell if the user has been authenticated. This does not imply any permissions, and doesn’t check if the user is active or has a valid session.
您可以通过多种方式实现这一目标,但 none 是“内置”方式。
This question covers the last activity time of a user,您可以使用它来检查用户在过去几分钟内是否处于活动状态。
Alternatively, you can query the Session table 检查用户是否有活动会话,但如果会话超时时间长,这可能不准确。
是的。但是检查用户是否登录的正确方法是使用:request.user.is_authenticated。如果此人以其他方式登录,这将 return 为真 False。
例如:
在模板中:
{% if request.user.is_authenticated ==True %}
do something awesome.
在视图中将请求传递到模板中。
return render('url/filename.html',{'any variable name':request})
感谢 this 优秀的博客 post,稍作修改,我想出了一个更好的解决方案,它使用内存缓存,因此每个请求的延迟更少:
在models.py中添加:
from django.core.cache import cache
import datetime
from myproject import settings
并将这些方法添加到用户配置文件 class:
def last_seen(self):
return cache.get('seen_%s' % self.user.username)
def online(self):
if self.last_seen():
now = datetime.datetime.now()
if now > self.last_seen() + datetime.timedelta(
seconds=settings.USER_ONLINE_TIMEOUT):
return False
else:
return True
else:
return False
在 userprofile 文件夹中添加此 middleware.py
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 = datetime.datetime.now()
cache.set('seen_%s' % (current_user.username), now,
settings.USER_LASTSEEN_TIMEOUT)
在 settings.py 中将 'userprofile.middleware.ActiveUserMiddleware',
添加到 MIDDLEWARE_CLASSES
并添加:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
# Number of seconds of inactivity before a user is marked offline
USER_ONLINE_TIMEOUT = 300
# 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
并在 profile.html 中:
<table>
<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>{{ profile.online }}</td></tr>
</table>
就是这样!
在控制台中测试缓存,确保内存缓存正常工作:
$memcached -vv
$ python manage.py shell
>>> from django.core.cache import cache
>>> cache.set("foo", "bar")
>>> cache.get("foo")
'bar'
>>> cache.set("foo", "zaq")
>>> cache.get("foo")
'zaq'
正如documentation所说:
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.
所以要检查用户是否在线,我会做这样的事情:
models.py
class Profile(models.Model):
user = models.OneToOneField(User, related_name='profile')
is_online = models.BooleanField(default=False)
views.py
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.dispatch import receiver
@receiver(user_logged_in)
def got_online(sender, user, request, **kwargs):
user.profile.is_online = True
user.profile.save()
@receiver(user_logged_out)
def got_offline(sender, user, request, **kwargs):
user.profile.is_online = False
user.profile.save()
然后在您的模板中检查用户是否在线:
{% if user.profile.is_online %}
Online
{% else %}
Offline
{% endif %}
不要忘记将 return 用户实例添加到您的模板中,以便 user.profile.is_online
正常工作。
首先通过 运行
安装 django-online-users
pip install django-online-users
然后在你的 settings.py
INSTALLED_APPS = [
...
'online_users',]
MIDDLEWARE_CLASSES = (
...
'online_users.middleware.OnlineNowMiddleware',)
那么在你看来
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"}`
例如将上下文传递给您的模板
{% for user in users %}
{{user.user}}
{% endfor %}
这将输出最近 60 秒内活跃和在线的用户
在模板中,当我使用
{% if topic.creator.is_authenticated %}
Online
{% else %}
Offline
{% endif %}
用户总是在线,即使他们刚刚退出。所以我想知道如何正确查看在线用户数?
您可以为每个用户设置一个整数字段,说明该用户当前登录的会话数。您可以在用户每次在某处登录时将其增加 1,并在用户在某处注销时将其减少 1。
{% if topic.creator.login_count %}
Online
{% else %}
Offline
{% endif %}
这是解决这个问题的简单方法。您可以定期通过 ajax 请求刷新此数据。
is_authenticated()
- Always returns
True
... This is a way to tell if the user has been authenticated. This does not imply any permissions, and doesn’t check if the user is active or has a valid session.
您可以通过多种方式实现这一目标,但 none 是“内置”方式。
This question covers the last activity time of a user,您可以使用它来检查用户在过去几分钟内是否处于活动状态。
Alternatively, you can query the Session table 检查用户是否有活动会话,但如果会话超时时间长,这可能不准确。
是的。但是检查用户是否登录的正确方法是使用:request.user.is_authenticated。如果此人以其他方式登录,这将 return 为真 False。
例如:
在模板中:
{% if request.user.is_authenticated ==True %}
do something awesome.
在视图中将请求传递到模板中。
return render('url/filename.html',{'any variable name':request})
感谢 this 优秀的博客 post,稍作修改,我想出了一个更好的解决方案,它使用内存缓存,因此每个请求的延迟更少:
在models.py中添加:
from django.core.cache import cache
import datetime
from myproject import settings
并将这些方法添加到用户配置文件 class:
def last_seen(self):
return cache.get('seen_%s' % self.user.username)
def online(self):
if self.last_seen():
now = datetime.datetime.now()
if now > self.last_seen() + datetime.timedelta(
seconds=settings.USER_ONLINE_TIMEOUT):
return False
else:
return True
else:
return False
在 userprofile 文件夹中添加此 middleware.py
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 = datetime.datetime.now()
cache.set('seen_%s' % (current_user.username), now,
settings.USER_LASTSEEN_TIMEOUT)
在 settings.py 中将 'userprofile.middleware.ActiveUserMiddleware',
添加到 MIDDLEWARE_CLASSES
并添加:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
# Number of seconds of inactivity before a user is marked offline
USER_ONLINE_TIMEOUT = 300
# 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
并在 profile.html 中:
<table>
<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>{{ profile.online }}</td></tr>
</table>
就是这样!
在控制台中测试缓存,确保内存缓存正常工作:
$memcached -vv
$ python manage.py shell
>>> from django.core.cache import cache
>>> cache.set("foo", "bar")
>>> cache.get("foo")
'bar'
>>> cache.set("foo", "zaq")
>>> cache.get("foo")
'zaq'
正如documentation所说:
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.
所以要检查用户是否在线,我会做这样的事情:
models.py
class Profile(models.Model):
user = models.OneToOneField(User, related_name='profile')
is_online = models.BooleanField(default=False)
views.py
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.dispatch import receiver
@receiver(user_logged_in)
def got_online(sender, user, request, **kwargs):
user.profile.is_online = True
user.profile.save()
@receiver(user_logged_out)
def got_offline(sender, user, request, **kwargs):
user.profile.is_online = False
user.profile.save()
然后在您的模板中检查用户是否在线:
{% if user.profile.is_online %}
Online
{% else %}
Offline
{% endif %}
不要忘记将 return 用户实例添加到您的模板中,以便 user.profile.is_online
正常工作。
首先通过 运行
安装 django-online-userspip install django-online-users
然后在你的 settings.py
INSTALLED_APPS = [
...
'online_users',]
MIDDLEWARE_CLASSES = (
...
'online_users.middleware.OnlineNowMiddleware',)
那么在你看来
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"}`
例如将上下文传递给您的模板
{% for user in users %}
{{user.user}}
{% endfor %}
这将输出最近 60 秒内活跃和在线的用户