Django的在线人数 [英] Number of online people in django

查看:83
本文介绍了Django的在线人数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,我想在我的网站中找到在线用户的数量(是否所有用户都登录到他们的帐户):并且当他们下线或关闭页面时,在线用户的数量会更新:我该怎么办?有没有图书馆来做到这一点?

解决方案

您可以将 is_online BooleanField添加到用户模型,并创建自己的自定义验证 后端用于您的用户模型,该模型在身份验证后设置了 is_online 为true.

当用户关闭网页时,您可以让事件侦听器发出Ajax请求,然后在数据库中将用户 is_online 列设置为false并注销用户./p>

如果您不想从会话中删除用户,则可能需要在其他地方使用其他逻辑,以便当他们回到网站时将该属性设置为true.

 //或window.addEventListener("beforeunload",function(e){//做一点事}, 错误的); 

然后您可以通过执行 model.objects.filter(is_online = True)找到您的在线用户,或者您可以在Model本身上创建一个自定义方法,该方法可以包含其他业务逻辑,或者可能会发送电子邮件或其他内容.

I have a website and i want to find number of online users in my website(All of users logged to their account or not): And when they get offline or close page the number of online users get update: How can i do that?is there any library to do that?

解决方案

You can add an is_online BooleanField to your User model and create your own custom authenticate backends for your User model that after authentication sets the is_online to true.

When a user goes to close the web page you can have a event listener to make an Ajax request where you will then set that users is_online column to false in DB and logs the user out.

If you don't want to remove the user from sessions you might need additional logic in other places to turn that attribute to true when they come back to the website.

// OR
window.addEventListener("beforeunload", function(e){
   // Do something
}, false);

You can then find your online users by either doing a model.objects.filter(is_online=True) or you can create a custom method on the Model itself that can contain other business logic and or potentially send a email or something.

这篇关于Django的在线人数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆