简体   繁体   English

django中间件重定向无限循环

[英]django middleware redirect infinite loop

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. 我的问题是,它正在创建一个无限重定向循环,但我不确定为什么。

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. 因此,我要检查的是可见会话的值是否为yes,否则将用户重定向到我的测试视图。

Here is my middleware: 这是我的中间件:

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"))

But the more descent way seems to be using the process_view -method ! 但是,更为下降的方法似乎是使用process_view -method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM