Number of online people in django [closed]

独自空忆成欢 提交于 2021-02-11 12:15:40

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/44978609/number-of-online-people-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!