关闭浏览器后,django中的会话未过期

时间:2020-10-12 12:37:55

标签: django django-sessions

我是django和python的新手。用户关闭网络浏览器但未注销后,会话期满出现问题。我可以看到,即使等待了10分钟以上,会话仍在会话表中保持活动状态。我更新了自定义的middleware.py和setting.py文件,如下所示:

    middleware.py
    from django.contrib.auth import logout
    from django.contrib import messages
    import datetime
    import syslog
    class SessionIdleTimeout(deprecation.MiddlewareMixin if DJANGO_VERSION >= (1, 10, 0) else object):
   def process_request(self, request):
   if request.user.is_authenticated():
       current_datetime = datetime.datetime.now()
       syslog.syslog("This user is authenticated")
       if ('last_login' in request.session):
           last = (current_datetime - request.session['last_login']).seconds
           syslog.syslog("there is last_login present in request.session.")
           if last > settings.SESSION_IDLE_TIMEOUT:
               syslog.syslog("calling loggout since last > SESSION_IDLE_TIMEOUT")
               logout(request, login.html)
       else:
           syslog.syslog("no logout yet")
           request.session['last_login'] = current_datetime
   return None


 In setting.py
 MIDDLEWARE = [
 ..
 'webapp.middleware.SessionIdleTimeout',
 ..

 SESSION_EXPIRE_AT_BROWSER_CLOSE = True
 INACTIVE_TIME = 10*60  # 10 minutes - or whatever period you think appropriate
 SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
 SESSION_EXPIRE_AT_BROWSER_CLOSE= True
 SESSION_COOKIE_AGE = INACTIVE_TIME   # change expired session
 SESSION_IDLE_TIMEOUT = INACTIVE_TIME  # logout
 SESSION_TIMEOUT_REDIRECT = '/logout/'
 SESSION_SAVE_EVERY_REQUEST = True

这些是我的python和django版本: Python 2.7.9 django.VERSION(1,11,4,u'final',0)

有人可以指出以上代码中仍然缺少的内容吗?

0 个答案:

没有答案