APIView和View的区别
APIView和View的区别
API继承了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请求的数据