中间件没有预期的工作 [英] middleware is not work expected

查看:99
本文介绍了中间件没有预期的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,我试图实现一个中间件,它检测被认证的用户是否处于停用状态5秒钟。我已经在Python模块下面写了这个工作,但是看起来效果不好。我发现两个原因
其中之一是我无法正确地将用户重定向到主页
;中间件不正确更改会话密钥



我没有发现我如何解决这个问题。我将以下面的两个方面展示我所做的一切。



第一部分; middleware.py

  class TimeOut:
@csrf_exempt
def process_request(self,request):
try:
如果request.session ['isA'] == False:
return #redirect(reverse(homePage_view))
除了KeyError:
request.session ['isA'] = False
return
try:
passT = datetime.now() - request.session ['Time']
如果passT> timedelta(0,settings.SESSION_COOKIE,0):
request.session ['isA'] = False
del request.session ['Time']
return
除了KeyError:
pass
request.session ['Time'] = datetime.now()



<第二部分; settings.py

  SESSION_COOKIE = 5 

MIDDLEWARE_CLASSES =(
'home.middleware。 TimeOut',

编辑:我错误地写了其他类。我改名为TimeOut

解决方案

这是你正在说的话:

  class AutoLogout:
def process_request(self,request):
if not request.user.is_authenticated():
return HttpResponseRedirect reverse('app_name:url_name'))

try:
如果datetime.now() - request.session ['last_touch']> timedelta(0,settings.AUTO_LOGOUT_DELAY * 60,0):
auth.logout(request)
del request.session ['last_touch']
return HttpResponseRedirect(reverse('app_name:url_name' ))
除了KeyError:
pass

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

decorators.py

  django.core.urlresolvers import reverse 
from django.http import HttpResponseRedirect

def login_check(view_func):
def _wrapped_view_func(request,* args,** kwargs):
如果没有request.user.is_authenticated:
//返回主页url
返回HttpResponseRedirect(reverse('app_name:url_name'))
return view_func(request,* args,* * kwargs)
return _wrapped_view_func

创建decorators.py后,更新您的视图,如下所示

  from app_name.decorators import login_check 

@login_check
def view_name(request):
.........

用户不会被允许去该页面如果没有认证。


I have some problem related to that I am trying to implement a middleware which detects the whether the authenticated user is inactive for 5 seconds. I have wrote below Python module to do this job but It seems it is not works well. I found two reason ; One of them is ; I can not redirect the user to the home page correctly ; Middleware is not change session key correctly

I have not found that how I can solve this problems. I will show what I have done to the below as two part.

First part ; middleware.py

class TimeOut:
    @csrf_exempt 
    def process_request(self, request):
        try :
            if request.session['isA'] == False:
                return #redirect(reverse("homePage_view"))
        except KeyError:
            request.session['isA'] = False
            return
        try :
            passT = datetime.now() - request.session['Time'] 
            if passT > timedelta( 0, settings.SESSION_COOKIE, 0):
                request.session['isA'] = False
                del request.session['Time']
                return
        except KeyError:
            pass
        request.session['Time'] = datetime.now()

Second part ; settings.py

SESSION_COOKIE = 5 

MIDDLEWARE_CLASSES = (
    'home.middleware.TimeOut',
)

EDIT: I have mistakenly wrote other class. I have changed the name as TimeOut

解决方案

Is this the one you are talking:

class AutoLogout:
    def process_request(self, request):
        if not request.user.is_authenticated() :
            return HttpResponseRedirect(reverse('app_name:url_name'))

        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 HttpResponseRedirect(reverse('app_name:url_name'))
        except KeyError:
            pass

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

decorators.py

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

def login_check(view_func):
    def _wrapped_view_func(request, *args, **kwargs):
        if not request.user.is_authenticated:
            //return to home page url
            return HttpResponseRedirect(reverse('app_name:url_name'))
        return view_func(request, *args, **kwargs)
    return _wrapped_view_func

After you create decorators.py, update your view like this:

from app_name.decorators import login_check

@login_check
def view_name(request):
    .........

The user will not be allow to go to that page if not authenticated.

这篇关于中间件没有预期的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆