Django1.10自动注销

时间:2017-03-12 12:23:55

标签: django

我使用了如下所示的包含代码,以便在5分钟后自动注销。 但我收到了错误

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 82, in load_middleware
mw_instance = middleware(handler)
TypeError: this constructor takes no arguments

我的代码

在settings.py中

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'

在middleware.py

from datetime import datetime, timedelta
from django.conf import settings
from django.contrib import auth


class AutoLogout:
  def process_request(self, request):
    if not request.user.is_authenticated() :
      #Can't log out if not logged in
      return

    try:
        if datetime.now() - request.session['last_touch'] > timedelta( 0, settings.AUTO_LOGOUT_DELAY * 60, 0):
            auth.logout(request)
            del request.session['last_touch']
            return
    except KeyError:
        pass

    request.session['last_touch'] = datetime.now()

在settings.py中:

MIDDLEWARE_CLASSES = [
    .........................
    'app_name.middleware.AutoLogout', 
]

以分钟为单位的自动注销延迟

AUTO_LOGOUT_DELAY = 5  # equivalent to 5 minutes

1 个答案:

答案 0 :(得分:0)

您是否尝试过新的middleware syntax

这样的事情:

id 2