How to set sessions timeout in django?

Question:

I want to implement login and logout session in my website through which after a set of time the session should expire automatically. And if user logged in then the user could not go back.

Asked By: Mandeep Thakur

||

Answers:

In your settings.py set https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-SESSION_COOKIE_AGE.

For example if you want time out to be one hour

SESSION_COOKIE_AGE = 3600 # one hour in seconds
Answered By: Sardorbek Imomaliev

To "settings.py", set SESSION_COOKIE_AGE which is 1209600 seconds(2 weeks) by default and SESSION_SAVE_EVERY_REQUEST which is "False" by default as shown below:

# "settings.py"

SESSION_COOKIE_AGE = 180 # 3 minutes. "1209600(2 weeks)" by default

SESSION_SAVE_EVERY_REQUEST = True # "False" by default

If SESSION_SAVE_EVERY_REQUEST is "True", users are logged out if inactive.

If SESSION_SAVE_EVERY_REQUEST is "False", users are logged out whether active or inactive.

Answered By: Kai – Kazuya Ito