django signals are very handy :
# 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 :
*/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 :
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.