django中间件重定向无限循环 [英] django middleware redirect infinite loop

查看:89
本文介绍了django中间件重定向无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中间件,可以检查会话值并根据该值进行重定向.我的问题是,它正在创建一个无限重定向循环,但我不确定为什么.

I have a middleware that checks a session value and redirects depending that value. My problem is, it is creating an infinite redirect loop and I'm not sure why.

因此,我要检查的是可见会话的值是否为yes,否则将用户重定向到我的测试视图.

So, what I want to do is check to see if the value of the session visible is yes and if not redirect the user to my test view.

这是我的中间件:

class CheckStatus(object):  

    def process_request(self, request):    
        if request.user.is_authenticated():                

                s = request.session.get('visible')
                if str(s) is not 'yes':
                    return HttpResponseRedirect(reverse("myapp.myview.views.test"))

推荐答案

在提供某些媒体文件时,您至少应避免使其运行:

You should at least avoid having it run when serving some media files:

from django.conf import settings

class CheckStatus(object):  

    def process_request(self, request):    
        if request.user.is_authenticated():                
           if not request.path.startswith(settings.MEDIA_URL):
                s = request.session.get('visible')
                if str(s) is not 'yes':
                    return HttpResponseRedirect(reverse("myapp.myview.views.test"))

但更为下降的方法似乎是使用 process_view -方法

But the more descent way seems to be using the process_view-method!

这篇关于django中间件重定向无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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