我使用了如下所示的包含代码,以便在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