I am implementing a basic login and logout page using django. My login function adds a row into the django_session table in db. However, when I logout, it doesn’t remove the session row. Since the session is no longer valid and all the session related data is removed from the request, shouldn’t the logout function also remove the session row from the django_session table?
Here is my logout function:
@login_required
def logout_student(request):
logout(request)
# Redirect to a success page.
return HttpResponseRedirect('/index/')
Thanks for your help.
If you manually adds a row to
django_sessionon login, then ondjango.contrib.auth.logout(), therequest.session.flush()) function will only delete the row with the same primary keysession_keyas the current session key fromdjango_sessiontable.request.session.flush()is used to ensure that the previous session data can’t be accessed again from the user’s browser. It basically does two things:The Django source code of
django.contrib.auth.logout():Delete method for database-based session:
To remove the manually added row, you can utilize Django signal
django.contrib.auth.signals.user_logged_outto delete row on user logout.