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?
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