Django会话的默认行为 - 关闭浏览器后Session会过期

时间:2015-08-14 06:38:03

标签: django python-2.7 session authentication django-sessions

我阅读了Django教程但发现与永不过期的会话无关。

要求 - 用户只有在他/她通过点击退出启动时才会退出。

我该如何解决这个问题? 我的django项目设置与会话相关 -

INSTALLED_APPS = (
    ..
    'django.contrib.sessions',
    ..
)
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        #'rest_framework.authentication.SessionAuthentication',
    )
}
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)

1 个答案:

答案 0 :(得分:3)

默认情况下,Django会在浏览器关闭之间保持会话。您可以使用SESSION_EXPIRE_AT_BROWSER_CLOSE设置修改此行为。

https://docs.djangoproject.com/en/1.8/topics/http/sessions/#browser-length-vs-persistent-sessions