我想要实现的是,如果子域(租户)已注册,那么它应该显示另一个页面,否则执行正常。
我只是想用自定义中间件运行django。截至目前,get_response在显示主页时返回500状态。
以下例外会返回'NoneType' object is not callable
如果用户是普通用户而不是租户,我希望该应用程序正常运行。
我刚刚开始,但我认为get_response函数是显示其他中间件执行的响应,以后会导致500状态。
任何帮助试图找出,我如何使用我的自定义中间件是值得赞赏的。
class TenantMiddleware(MiddlewareMixin):
def process_request(self, request):
absolute_url = request.get_full_path()
url = urlparse.urlparse(absolute_url)
print absolute_url
print url
subdomain = url.hostname.split(".") if url.hostname else None
if subdomain and subdomain != 'affectlab' :
if 'subdomain' not in request.session:
print "I am here"
tenant=map(lambda x : x[0],(list(profiles.models.Tenant.objects.filter(name=subdomain).values_list('id'))))
if tenant:
request.session['tenant'] = int(tenant[0])
request.session['subdomain'] = subdomain.strip()
return HttpResponseRedirect("/login/")
else:
response = render_to_response('404.html', {},context_instance=RequestContext(request))
response.status_code = 404
return response
else:
if request.session['subdomain'] == subdomain:
return self.get_response(request)
else:
tenant = map(lambda x: x[0],(list(profiles.models.Tenant.objects.filter(name=subdomain)).values_list('id')))
if tenant:
request.session['tenant'] = int(tenant[0])
request.session['subdomain'] = subdomain.strip()
response = logout(request) # ,next_page=reverse("/"))
return HttpResponseRedirect("/login/")
else:
response = render_to_response('404.html', {}, context_instance=RequestContext(request))
response.status_code = 404
return response
else:
try :
response = self.get_response(request)
except Exception as e:
print e.message
return response
答案 0 :(得分:0)
由于我使用的是Django1.10,我似乎必须在设置文件中使用MIDDLEWARE而不是MIDDLEWARE_CLASSES。
只需使用MIDDLEWARE。
我们也可以这样写。
class TenantMiddleware(object):
def __init__(self,get_response):
self.get_response = get_response
def __call__(self, request):
absolute_url = request.get_full_path()
url = urlparse.urlparse(absolute_url)
print absolute_url
print url
subdomain = url.hostname.split(".") if url.hostname else None
if subdomain and subdomain != 'affectlab' :
if 'subdomain' not in request.session:
print "I am here"
tenant=map(lambda x : x[0],(list(profiles.models.Tenant.objects.filter(name=subdomain).values_list('id'))))
if tenant:
request.session['tenant'] = int(tenant[0])
request.session['subdomain'] = subdomain.strip()
return HttpResponseRedirect("/login/")
else:
response = render_to_response('404.html', {},context_instance=RequestContext(request))
response.status_code = 404
return response
else:
if request.session['subdomain'] == subdomain:
return self.get_response(request)
else:
tenant = map(lambda x: x[0],(list(profiles.models.Tenant.objects.filter(name=subdomain)).values_list('id')))
if tenant:
request.session['tenant'] = int(tenant[0])
request.session['subdomain'] = subdomain.strip()
response = logout(request) # ,next_page=reverse("/"))
return HttpResponseRedirect("/login/")
else:
response = render_to_response('404.html', {}, context_instance=RequestContext(request))
response.status_code = 404
return response
else:
try :
response = self.get_response(request)
except Exception as e:
print e.message1
return response