Skip to content Skip to sidebar Skip to footer

Django Create Session Which Does Not Destroy After Logout

In my django project I want a session which only destroys after a certain time, for that I set the expire time but session is also destroying after logout. Basically what I want to

Solution 1:

Well, when you call logout, it flushes the session. If you want to keep that data, then you need to define your own logout functionality. You can try like that:

from django.contrib.auth import logout

deflogout(request):
    your_data = request.session.get('your_key', None)
    current_expiry = request.session.get('_session_expiry')
    logout(request)
    if your_data:
        request.session['your_key'] = your_data
        if current_expiry:
           request.session['_session_expiry'] = current_expiry

FYI Its an untested code. Also, Maybe its better if you don't use session for storing data which should last after logout. You can use redis or any temporary storage for this.

Solution 2:

Try This:

Views.py

from django.contrib.auth import logout

deflogoutUser(request):
    logout(request)
    messages.success(request,"Successfully logged out")
    return redirect("login")

Post a Comment for "Django Create Session Which Does Not Destroy After Logout"