Django 会话过期?

发布于 2024-08-03 19:34:22 字数 377 浏览 8 评论 0原文

从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光病人 2024-08-10 19:34:22

作为这些方法的作者,我可以看到文档对此并不是很清楚。您的观察是正确的:只有导致会话更改的请求才被视为“活动”。

您可以使用 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.

七七 2024-08-10 19:34:22

一个简单的中间件可能比在每个视图中设置它更好。这就是我用的。

class SessionExpiry(object):
    """ Set the session expiry according to settings """
    def process_request(self, request):
        if getattr(settings, 'SESSION_EXPIRY', None):
            request.session.set_expiry(settings.SESSION_EXPIRY)
        return None

这取决于您的配置中设置的 SESSION_EXPIRY。它的格式与request.session.set_expiry相同。

MIDDLEWARE_CLASSES 的定义应牢记以下顺序:

MIDDLEWARE_CLASSES = (
    ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    '<yourproject>.<yourapp>.middleware.SessionExpiry',
    ...
}

如果 django.contrib.sessions 默认考虑此设置,那就太好了。

A simple middleware would probably do better than setting this up in every view. This is what I used.

class SessionExpiry(object):
    """ Set the session expiry according to settings """
    def process_request(self, request):
        if getattr(settings, 'SESSION_EXPIRY', None):
            request.session.set_expiry(settings.SESSION_EXPIRY)
        return None

This depends on SESSION_EXPIRY being set in your config. It's format is the same as request.session.set_expiry.

MIDDLEWARE_CLASSES should be defined with this order in mind:

MIDDLEWARE_CLASSES = (
    ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    '<yourproject>.<yourapp>.middleware.SessionExpiry',
    ...
}

It'd be nice if django.contrib.sessions took this setting into account by default.

如歌彻婉言 2024-08-10 19:34:22

如果将“True”设置为"settings.py" 上的SESSION_SAVE_EVERY_REQUEST 如下所示,每次当前页面打开时,会话都会自动更新重新打开或在 Django 网站 中打开其他页面。

SESSION_SAVE_EVERY_REQUEST = True # False by default

例如,会话将在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.

SESSION_SAVE_EVERY_REQUEST = True # False by default

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文