Home  >  Article  >  Backend Development  >  How to set session expiration time with django

How to set session expiration time with django

一个新手
一个新手Original
2017-09-09 13:34:502815browse

Session timeout setting

settings

SESSION_COOKIE_AGE=60*30 30分钟。
SESSION_EXPIRE_AT_BROWSER_CLOSE False:会话cookie可以在用户浏览器中保持有效期。True:关闭浏览器,则Cookie失效。
SESSION_COOKIE_DOMAIN 生效站点
SESSION_COOKIE_NAME cookie中保存session的名称

Session is relatively simple to use. request.session is a dictionary class. Sessions are saved in the database.

-------------------------------- -----------------------

按手册和网上的方法在settings.py中设置“SESSION_COOKIE_AGE” 和 “SESSION_EXPIRE_AT_BROWSER_CLOSE” 均不生效。 通过查看django的源代码"middleware.py"才知道这两个参数只有在settings.SESSION_SAVE_EVERY_REQUEST
 为True时才有效。依此在settings.py中设置这个变量后问题解决。
从源代码看SESSION_EXPIRE_AT_BROWSER_CLOSE为True时 SESSION_COOKIE_AGE 不生效。也就是说用户只能二选一,在浏览器关闭时使session失效 或 超时失效。

---------------------------------------- -----------------------

#The following method is not used in settings

##

request.session.set_expiry(value)

You can pass four different values ​​to it:

* If value is an integer, the session will expire after a number of seconds (applicable to the entire Django framework, that is, the session will expire on the entire page when this value expires).

* If value is a datatime or timedelta, the session will expire after this time.

* If value is 0, the session will become invalid when the user closes the browser.

* If value is None, session will rely on the global session invalidation policy.

The above is the detailed content of How to set session expiration time with django. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn