[Django]-Django sessions – keeping the session data between login/logout

2👍

You have to use the database-backed session. From the doc:

you need to add ‘django.contrib.sessions’ to your INSTALLED_APPS
setting.

Once you have configured your installation, run manage.py migrate to install the single database table that stores session data.

Then you have to ensure that the session.flush() is not called in the logout/login process, witch implies avoid using the django.contrib.auth.logout() witch will call session.flush(), it is also called in django.contrib.login(). login and logout the user yourself to avoid losing the session data. source for login/logout.

1👍

The session is flushed at login/logout, as a security measure. If you want to retain some variables, you can use the solution at:

https://stackoverflow.com/a/41849076/146289

It basically involves backing up old values, and then restoring them in the new session.

👤vdboor

Leave a comment