python Django会话到期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1366146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:03:59  来源:igfitidea点击:

Django session expiry?

pythondjango

提问by slypete

From django's documentation, I became under the impression that calling:

从 django 的文档中,我的印象是这样调用:

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 a expire only after five minutes of inactivity and not simply failing to call set_expiry again before the expiry.

从一种观点来看,会导致会话在 5 分钟不活动后过期;但是,这不是我在 django 主干中遇到的行为。如果我从一个视图调用此方法,并浏览其他未调用该方法的视图,则会话将在五分钟后到期。我期望的行为是仅在五分钟不活动后过期,而不是在过期前再次调用 set_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.

我的问题是我真的需要在每个视图中调用 set_expiry 吗?如果是这样,是否存在一些可能有帮助的装饰器?我无法想象这不是 contrib 的一部分。

Thanks, Pete

谢谢,皮特

回答by SmileyChris

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_REQUESTsetting to get the behavior you're after (at the obvious cost of the session having to being saved every request).

您可以使用该SESSION_SAVE_EVERY_REQUEST设置来获得您所追求的行为(明显的代价是必须保存每个请求的会话)。

Note : It will update the existing session record with latest expiry date.

注意:它将更新现有会话记录的最新到期日期。

回答by Mike Shultz

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_EXPIRYbeing set in your config. It's format is the same as request.session.set_expiry.

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

MIDDLEWARE_CLASSESshould be defined with this order in mind:

MIDDLEWARE_CLASSES应该按照以下顺序定义:

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

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

如果django.contrib.sessions默认情况下考虑此设置会很好。