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请求的数据

posted on 2018-11-16 14:49  窮山霧繞(静妙)  阅读(758)  评论(0编辑  收藏  举报

导航