在django中,如何使用request.session.set_expiry在空闲后注销用户?
我想在一段时间不活动后注销用户。这个问题(将用户从N 分钟不活动后的 Django 站点)有一个看起来合理的答案。
但我想了解 request.session.set_expiry 与 SESSION_COOKIE_AGE 的区别。前者似乎会在固定时间后将用户注销,而不管其活动如何。如果 SESSION_SAVE_EVERY_REQUEST 为 False,这不也是 SESSION_COOKIE_AGE 所做的吗?
I want to log users out after some period of inactivity. This question (Logging users out of a Django site after N minutes of inactivity) has a reasonable looking answer.
But I'd like to understand what distinguishes request.session.set_expiry from SESSION_COOKIE_AGE. The former seems to log the user out after a fixed period regardless of activity. Isn't this also what SESSION_COOKIE_AGE does if SESSION_SAVE_EVERY_REQUEST is False?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,
request.session.set_expiry
只是覆盖了该特定会话的SESSION_COOKIE_AGE
设置。使用SESSION_SAVE_EVERY_REQUEST = False
(默认值),不会有功能差异。在这两种情况下,会话活动都基于上次修改会话的时间(除非
SESSION_SAVE_EVERY_REQUEST
为True
,在这种情况下,它会在每个请求上保存,因此会有效地修改 一个示例是,您可能希望应用程序某一部分中的用户拥有更长的会话过期时间,因此您可以使用
request.session.set_expiry
以及相关视图中的自定义值到该应用程序,然后在离开该特定部分时使用request.session.set_expiry(SESSION_COOKIE_AGE)
重置它。From what I can tell,
request.session.set_expiry
simply overrides theSESSION_COOKIE_AGE
setting for that specific session. WithSESSION_SAVE_EVERY_REQUEST = False
(the default), there would be no functional difference.In both cases, session activity is based off of when the session was last modified (unless
SESSION_SAVE_EVERY_REQUEST
isTrue
, in which case it is saved on every request, so it's effectively modified on every request)One example is that you may want users in a certain section of your application to have a longer session expiration, so you could use
request.session.set_expiry
with a custom value in the views related to that application, and then reset it withrequest.session.set_expiry(SESSION_COOKIE_AGE)
when they leave that particular section.set_expiry() 覆盖 <强>SESSION_COOKIE_AGE。换句话说,如果 set_expiry() 被执行,set_expiry() 优先而不是 SESSION_COOKIE_AGE。
如果SESSION_SAVE_EVERY_REQUEST 为“True”,如果不活动,用户将注销。
如果SESSION_SAVE_EVERY_REQUEST 为“False”,则无论活动还是非活动,用户都会注销。
set_expiry() overrides SESSION_COOKIE_AGE. In other words, if set_expiry() is executed, set_expiry() is prioritized rather than SESSION_COOKIE_AGE.
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.