Web page has a redirect loop
I am developing this django app for users that I intend each user to have their own custom subdomain. I intend to use a middleware for this purpose. So far, this is what I got, but on accessing the site (on Google chrome), I get the redirect loop error:
class SubdomainMiddleware(object):
def process_request(self, request):
if request.user.is_authenticated():
if request.company is None:
company_subdomain = request.user.company.company_url
else:
company_subdomain = request.company.company_url
company_subdomain = company_subdomain.lower()
domain_parts = request.get_host().split('.')
if len(domain_parts) > 2:
if domain_parts[0].lower() != company_subdomain:
domain_parts[0] = company_subdomain
else:
domain_parts.insert(0, company_subdomain)
domain = '.'.join(domain_parts)
return HttpResponseRedirect('http://' + domain + request.get_full_path())
else:
pass
What I'm I doing wrong?
Your first redirect is correct, but then you redirect again even though you are on the right domain. This results in the redirect loop. You need to check if the current subdomain matches the intended subdomain, and if it does, return None
instead of a redirect:
if len(domain_parts) > 2:
if domain_parts[0].lower() != company_subdomain:
domain_parts[0] = company_subdomain
else: # Subdomain matches, return instead of redirect
return None
else:
domain_parts.insert(0, company_subdomain)
链接地址: http://www.djcxy.com/p/33736.html
上一篇: 从子域获取会话信息到父域[Django]
下一篇: 网页有一个重定向循环