APIView源码分析
# from rest_framework.views import APIView # urls.py path('booksapiview/', views.BooksAPIView.as_view()), # 在这个地方应该写一个函数内存地址 # APIView的as_view方法(类的绑定方法) @classmethod def as_view(cls, **initkwargs): view = super().as_view(**initkwargs) # 调用父类(View)的as_view(**initkwargs) view.cls = cls # 参见下图补充 view.initkwargs = initkwargs # all other authentication is CSRF exempt. # 以后所有的请求都没有csrf的认证了,只要继承了APIView,就没有了csrf的认证 return csrf_exempt(view) # 请求来了--->路由匹配上--->View(request)--->调用了self.dispatch(),会执行apiview的dispatch # APIView的dispatch方法 def dispatch(self, request, *args, **kwargs): self.args = args self.kwargs = kwargs # self.initialize_request(request, *args, **kwargs) request是当次请求的request # request = self.initialize_request request是一个新的request对象 # 重新包装成一个request对象,以后再用request对象,就是新的request对象了 request = self.initialize_request(request, *args, **kwargs) self.request = request self.headers = self.default_response_headers # deprecate? try: # 三大认证模块(详情参见下述内容) self.initial(request, *args, **kwargs) # Get the appropriate handler method 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 response = handler(request, *args, **kwargs) except Exception as exc: response = self.handle_exception(exc) self.response = self.finalize_response(request, response, *args, **kwargs) return self.response views.py中 class BooksAPIView(APIView): def get(self, request): # request已经不是原生django的request了,是drf自己定义的request对象 # print(self.request) print(request.data) return HttpResponse('ok')
# from rest_framework.views import APIView
# urls.pypath('booksapiview/', views.BooksAPIView.as_view()), # 在这个地方应该写一个函数内存地址
# APIView的as_view方法(类的绑定方法) @classmethod def as_view(cls, **initkwargs):
view = super().as_view(**initkwargs) # 调用父类(View)的as_view(**initkwargs) view.cls = cls # 参见下图补充 view.initkwargs = initkwargs # all other authentication is CSRF exempt.# 以后所有的请求都没有csrf的认证了,只要继承了APIView,就没有了csrf的认证 return csrf_exempt(view) # 请求来了--->路由匹配上--->View(request)--->调用了self.dispatch(),会执行apiview的dispatch # APIView的dispatch方法 def dispatch(self, request, *args, **kwargs):
self.args = args self.kwargs = kwargs # self.initialize_request(request, *args, **kwargs) request是当次请求的request # request = self.initialize_request request是一个新的request对象 # 重新包装成一个request对象,以后再用request对象,就是新的request对象了 request = self.initialize_request(request, *args, **kwargs) self.request = request self.headers = self.default_response_headers # deprecate?
try: # 三大认证模块(详情参见下述内容) self.initial(request, *args, **kwargs)
# Get the appropriate handler method 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
response = handler(request, *args, **kwargs)
except Exception as exc: response = self.handle_exception(exc)
self.response = self.finalize_response(request, response, *args, **kwargs) return self.response
views.py中 class BooksAPIView(APIView): def get(self, request): # request已经不是原生django的request了,是drf自己定义的request对象 # print(self.request) print(request.data) return HttpResponse('ok')