Django 会话过期?
从 django 的文档中,我得到的印象是,
request.session.set_expiry(300)
从一个视图调用:将导致会话在五分钟不活动后过期;但是,这不是我在 django trunk 中遇到的行为。如果我从一个视图调用此方法,然后浏览到不调用该方法的其他视图,则会话将在五分钟后过期。我所期望的行为是仅在五分钟不活动后才到期,而不是简单地在到期前再次调用 set_expiry 失败。
我的问题是我真的需要在每个视图中调用 set_expiry 吗?如果是这样,是否存在一些可能有帮助的装饰器?我无法想象这不是贡献的一部分。
谢谢, 皮特
From django's documentation, I became under the impression that calling:
request.session.set_expiry(300)
from one view would cause the session to expire after five minutes inactivity; however, this is not the behavior that I'm experiencing in django trunk. If I call this method from one view, and browse around to other views that don't call the method, the session expires in five minutes. The behavior that I was expecting was an expiry only after five minutes of inactivity and not simply failing to call set_expiry again before the expiry.
My question then is do I really need to call set_expiry in every view? If so, does there exist some decorator that may be of assistance? I can't imagine this isn't part of contrib.
Thanks,
Pete
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为这些方法的作者,我可以看到文档对此并不是很清楚。您的观察是正确的:只有导致会话更改的请求才被视为“活动”。
您可以使用
SESSION_SAVE_EVERY_REQUEST
设置来获取您想要的行为(明显的代价是必须保存每个请求的会话)。
注意:它将用最新的到期日期更新现有的会话记录。
As the author of those methods, I can see that the documentation isn't very clear regarding this. Your observations are correct: only requests which cause the session to be altered is considered "activity".
You can use the
SESSION_SAVE_EVERY_REQUEST
setting to get the behavior you're after (at the obvious cost of the session having to being saved every request).Note : It will update the existing session record with latest expiry date.
一个简单的中间件可能比在每个视图中设置它更好。这就是我用的。
这取决于您的配置中设置的
SESSION_EXPIRY
。它的格式与request.session.set_expiry
相同。MIDDLEWARE_CLASSES
的定义应牢记以下顺序:如果
django.contrib.sessions
默认考虑此设置,那就太好了。A simple middleware would probably do better than setting this up in every view. This is what I used.
This depends on
SESSION_EXPIRY
being set in your config. It's format is the same asrequest.session.set_expiry
.MIDDLEWARE_CLASSES
should be defined with this order in mind:It'd be nice if
django.contrib.sessions
took this setting into account by default.如果将“True”设置为"settings.py" 上的SESSION_SAVE_EVERY_REQUEST 如下所示,每次当前页面打开时,会话都会自动更新重新打开或在 Django 网站 中打开其他页面。
例如,会话将在15 分钟后过期。然后,从下午 3:00 开始,会话通过登录Django 网站中的页面开始,因此会话于下午 3:15 到期。然后,在下午 3:10,重新打开当前页面或在Django 网站中打开其他页面,以便更新会话,以便 >新会话在下午3:25到期,这意味着您登录到下午3:25,换句话说,如果当前页面是未重新打开或未在 Django 网站 中打开其他页面,则新会话将于下午 3:25 到期,这意味着您已在 < strong>3:25 pm,因此您需要再次登录Django网站中的页面来开始新会话。
If you set "True" to SESSION_SAVE_EVERY_REQUEST on "settings.py" as shown below, automatically, session is updated every time the current page is reopened or other page is opened in Django Website.
For example, session expires in 15 minutes. Then, from 3:00 pm, a session starts by logging in the page in Django Website so the session expires at 3:15 pm. Then, at 3:10 pm, the current page is reopened or other page is opened in Django Website so the session is updated so the new session expires at 3:25 pm which means you are logged in until 3:25 pm, so in other words, if the current page is not reopened or other page is not opened in Django Website then the new session expires at 3:25 pm which means you are logged out at 3:25 pm so you need to log in again to the page in Django Website to start a new session.