FBV和CBV区别
CBV 内部dispatch 做了分发
FBV没办法用类的继承,多态
# DJango的View只做了CBV的分发
def dispatch(self, request, *args, **kwargs):
# GET POST
# BookView().get
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)
APIView 和View的区别
-- APIView继承了View
-- 重写了as_view方法
-- 豁免csrf
-- def dispatch(self, request, *args, **kwargs):
self.args = args
self.kwargs = kwargs
# 重新封装request
# 新的request变成 Request的实例化对象
# 旧的request变成了_request
request = self.initialize_request(request, *args, **kwargs)
self.request = request
。。。。。。
-- def initialize_request(self, request, *args, **kwargs):
parser_context = self.get_parser_context(request)
return Request(
request,
parsers=self.get_parsers(),
authenticators=self.get_authenticators(),
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)
-- Class Request(object):
self._request = 旧的request
def query_params(self):
return self._request.GET
def data(self):
return post请求的数据