[Fixed]-Delete session key when browser is refreshed ( Django )

1👍

There is no way in Django (I can think of) to know if a page has been refreshed or just starting a new session, page etc. You could do something with the request and Javascript.

If after a page has finished loading you want to just remove all sessions dependant where you want this to happen, look at Signals or just removing the sessions after the request has finished in your view.

Re: deleting all sessions…

flush() Deletes the current session data from the session and deletes the session cookie. The user will remain logged in. https://docs.djangoproject.com/en/1.8/topics/http/sessions/#django.contrib.sessions.backends.base.SessionBase.flush

If you just want to delete each session key (and keep the user logged in) then do:

for key in request.session.keys():
    del request.session[key]

This deletes each session in request.session.

Leave a comment