[Answer]-Run method on session expire Django

1👍

Django doesn’t do anything at all when the browser closes. Django doesn’t even know – how can it: the only time Django knows anything about what you do in the browser is when you make a request to the server, but closing the browser is the opposite of making a request.

Session expiry on browser close is an attribute of the session cookie, not anything that Django does. It just means that the cookie is set with a flag that tells the browser not to persist it when it closes. The actual session data remains in Django’s session store, and will do until you explicitly clear it, but is not accessible because the cookie has been removed.

So, the upshot of that is that there is no way to tell explicitly when a session ends. The only thing you can do is to send regular keepalive signals – eg via Ajax – while the session is open, and taken an action if you haven’t seen any for a while.

Leave a comment