简体   繁体   中英

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.

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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