django middleware redirect infinite loop

六月ゝ 毕业季﹏ 提交于 2019-12-11 08:07:03

问题


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

回答1:


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!



来源:https://stackoverflow.com/questions/3613385/django-middleware-redirect-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!