jdango中间件:
class Test(View):
def dispatch(self, request, *args, **kwargs):
#第一种方法
func = getattr(self,request.method.lower())#反射
ret = func(request,*args,**kwargs)
#第二种方法
ret = super(Test,self).dispatch(request, *args, **kwargs)
return ret
def get(self, request,*args,**kwargs):
return HttpResponse("GET")
def post(self, request,*args,**kwargs):
return HttpResponse("GET")
CBV原理:
- url->view方法->dispatch方法(反射执行其他:GET/POST/DELETE/PUT)
继承dispatch:
class Base(View):
def dispatch(self, request, *args, **kwargs):
print(before)
ret = super(Base,self).dispatch(request, *args, **kwargs)
print(after)
return ret
class Test(Base,View): #继承Base
def get(self, request,*args,**kwargs):
return HttpResponse("GET")
def post(self, request,*args,**kwargs):
return HttpResponse("GET")