How to set session expiration time with django

一个新手
Release: 2023-03-15 21:14:02
Original
2978 people have browsed it

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的名称
Copy after login

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失效 或 超时失效。
Copy after login

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

#The following method is not used in settings

##

request.session.set_expiry(value)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template