Django - 检测会话的开始和结束

时间:2021-07-07 01:39:08

I hope someone can help me with this.

我希望有人可以帮助我。

I am trying implement a 'number of users online' counter on the home page of my site. I remember in the bad old days of ASP I used to be able to keep a counter going with a session.onstart and session.onend.

我正在尝试在我的网站主页上实现一个“在线用户数”计数器。我记得在ASP过去的糟糕时期,我曾经能够通过session.onstart和session.onend保持计数器。

How do I do it in Django?

我如何在Django中做到这一点?

Cheers

干杯

Rich

丰富

4 个解决方案

#1


8  

django signals are very handy :

django信号非常方便:

# this is in a models.py file
from django.db.models.signals import pre_delete
from django.contrib.sessions.models import Session

def sessionend_handler(sender, **kwargs):
    # cleanup session (temp) data
    print "session %s ended" % kwargs.get('instance').session_key

pre_delete.connect(sessionend_handler, sender=Session)

you'll need to delete your session reguraly as they can stay in the database if the user doesnt click 'logout' which the case most often. just add this to a cron :

您需要定期删除会话,因为如果用户没有点击最常见情况下的“注销”,他们可以留在数据库中。只需将其添加到cron:

*/5 * * * * djangouser /usr/bin/python2.5 /home/project/manage.py cleanup

also i usually add this to my manage.py for easy settings.py path find :

另外我通常将它添加到我的manage.py中以便于settings.py路径查找:

import sys
import os
BASE_DIR = os.path.split(os.path.abspath(__file__))[0]
sys.path.insert(0, BASE_DIR)

SESSION_EXPIRE_AT_BROWSER_CLOSE works but only affects client cookies, not server-actives sessions IMHO.

SESSION_EXPIRE_AT_BROWSER_CLOSE有效但仅影响客户端cookie,而不影响服务器活动会话IMHO。

#2


5  

from django.contrib.sessions.models import Session
import datetime
users_online = Session.objects.filter(expire_date__gte = datetime.datetime.now()).count()

This only works, of course, if you're using database storage for Sessions. Anything more esoteric, like memcache, will require you roll your own.

当然,这只适用于Sessions的数据库存储。任何更深奥的东西,比如memcache,都需要你自己动手。

#3


1  

Sorry, I don't believe you could get an accurate count on ASP/IIS. It's simply not possible for a server to tell the difference between the user leaving the browser open on a site without doing anything, navigating away to a different page, or closing the browser completely.

对不起,我不相信你能准确掌握ASP / IIS。服务器根本不可能告诉用户在没有做任何事情的情况下让浏览器在网站上打开,导航到另一个页面或完全关闭浏览器之间的差异。

Even if the session cookie expires at browser close, that still doesn't tell the server anything - the browser has now closed, so what is going to let the server know? It's simply the client-side cookie that has expired.

即使会话cookie在浏览器关闭时到期,仍然没有告诉服务器任何东西 - 浏览器现在已关闭,那么让服务器知道什么呢?它只是客户端cookie已过期。

The best thing you can do is to estimate based on session expires, as Elf has suggested.

您可以做的最好的事情是根据会话到期进行估算,正如Elf建议的那样。

#4


1  

If you need to track the active users you can try http://code.google.com/p/django-tracking/

如果您需要跟踪活跃用户,可以尝试http://code.google.com/p/django-tracking/

#1


8  

django signals are very handy :

django信号非常方便:

# this is in a models.py file
from django.db.models.signals import pre_delete
from django.contrib.sessions.models import Session

def sessionend_handler(sender, **kwargs):
    # cleanup session (temp) data
    print "session %s ended" % kwargs.get('instance').session_key

pre_delete.connect(sessionend_handler, sender=Session)

you'll need to delete your session reguraly as they can stay in the database if the user doesnt click 'logout' which the case most often. just add this to a cron :

您需要定期删除会话,因为如果用户没有点击最常见情况下的“注销”,他们可以留在数据库中。只需将其添加到cron:

*/5 * * * * djangouser /usr/bin/python2.5 /home/project/manage.py cleanup

also i usually add this to my manage.py for easy settings.py path find :

另外我通常将它添加到我的manage.py中以便于settings.py路径查找:

import sys
import os
BASE_DIR = os.path.split(os.path.abspath(__file__))[0]
sys.path.insert(0, BASE_DIR)

SESSION_EXPIRE_AT_BROWSER_CLOSE works but only affects client cookies, not server-actives sessions IMHO.

SESSION_EXPIRE_AT_BROWSER_CLOSE有效但仅影响客户端cookie,而不影响服务器活动会话IMHO。

#2


5  

from django.contrib.sessions.models import Session
import datetime
users_online = Session.objects.filter(expire_date__gte = datetime.datetime.now()).count()

This only works, of course, if you're using database storage for Sessions. Anything more esoteric, like memcache, will require you roll your own.

当然,这只适用于Sessions的数据库存储。任何更深奥的东西,比如memcache,都需要你自己动手。

#3


1  

Sorry, I don't believe you could get an accurate count on ASP/IIS. It's simply not possible for a server to tell the difference between the user leaving the browser open on a site without doing anything, navigating away to a different page, or closing the browser completely.

对不起,我不相信你能准确掌握ASP / IIS。服务器根本不可能告诉用户在没有做任何事情的情况下让浏览器在网站上打开,导航到另一个页面或完全关闭浏览器之间的差异。

Even if the session cookie expires at browser close, that still doesn't tell the server anything - the browser has now closed, so what is going to let the server know? It's simply the client-side cookie that has expired.

即使会话cookie在浏览器关闭时到期,仍然没有告诉服务器任何东西 - 浏览器现在已关闭,那么让服务器知道什么呢?它只是客户端cookie已过期。

The best thing you can do is to estimate based on session expires, as Elf has suggested.

您可以做的最好的事情是根据会话到期进行估算,正如Elf建议的那样。

#4


1  

If you need to track the active users you can try http://code.google.com/p/django-tracking/

如果您需要跟踪活跃用户,可以尝试http://code.google.com/p/django-tracking/