I want a function be executed every time a session has expired. Like, recording people viewing specific web page, when user session timeout, decrease the count.
Asked
Active
Viewed 300 times
0
-
Are you using database-backed sessions? https://docs.djangoproject.com/en/1.5/topics/http/sessions/#using-database-backed-sessions – ElmoVanKielmo Apr 10 '14 at 09:25
-
@ElmoVanKielmo Yes, I do – dspjm Apr 10 '14 at 09:26
-
I think this would be helpful for you: http://stackoverflow.com/questions/4083426/django-detect-session-start-and-end – Eugene Soldatov Apr 10 '14 at 10:01
1 Answers
0
Instead of trying to execute a function when session expires, you should just query Django session tables from the database to count active sessions.
I don't remember the exact database schema but the query will look similar to:
SELECT COUNT(*) FROM django_session_table WHERE expires > NOW();
As I said, you have to alter the DB table and column names and also the function retrieving the current time is DBMS dependent - NOW() works in PostgreSQL. Please consider this query as a pseudocode then. At the moment I have no possibility to examine Django DB schema but since you use database-backed sessions you already have these in your database, so it should not be a problem to check DB table and column name.
ElmoVanKielmo
- 10,907
- 2
- 32
- 46