I have created logout def in python, in that def I am deleting session that I have created after login the user.
I wish to end/del session when the user closes the browser using(X) OR using(alt+4).
how do I call logout url in javascript when user close the browser.
Here is def logout:
@login_required
def logout(request):
username = request.session['username']
ip = settings.IP
headers = settings.HEADERS
del request.session['username']
Here is urls.py
from django.urls import path
from . import views
urlpatterns = [
path('logout', views.logout, name="logout"),
]
Just set the SESSION_EXPIRE_AT_BROWSER_CLOSE setting to true, so that Django's session cookies are only valid for the length of the browser session.
If SESSION_EXPIRE_AT_BROWSER_CLOSE is set to True, Django will use browser-length cookies – cookies that expire as soon as the user closes their browser.