Frage

I am writing custom middlware that I want to redirect to another page if a condition is met. My code looks like this:

class SettingHandler(object):  

    def process_view(self, request, view_func, view_args, view_kwargs): 
        if request.user.paid: 
            return view_func(request, *view_args, **view_kwargs)

        else:
            return HttpResponseRedirect(reverse('setting')) 

My urls.py looks like this:

url(r'^setting/$', 'customers.settings', name='setting'),

When the redirect condition is reached, the page doesn't render. The output in the console looks like this:

"GET /setting/ HTTP/1.1" 302 0
"GET /setting/ HTTP/1.1" 302 0
"GET /setting/ HTTP/1.1" 302 0
"GET /setting/ HTTP/1.1" 302 0
"GET /setting/ HTTP/1.1" 302 0
"GET /setting/ HTTP/1.1" 302 0
"GET /setting/ HTTP/1.1" 302 0
"GET /setting/ HTTP/1.1" 302 0
"GET /setting/ HTTP/1.1" 302 0

How can I redirect to the desired page?

War es hilfreich?

Lösung 2

The actual error from the browser was "redirect loop".

I put an exclusion case to get it out of the loop and all worked fine

Andere Tipps

This will fix your immediate issue:

if request.setting or request.path == '/setting/':

But it's pretty hacky. There's probably a better way, but it'll depend on what you are trying to accomplish - IOW, what is the condition you're testing?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top