# ModelViewSet继承View(django原生View) # APIView继承了View # 先读View # urls.py path('books1/', views.Books.as_view()), # 在这个地方应该写一个函数内存地址,views.Books.as_view()执行完,是一个函数内存地址,as_view是一个类方法,类直接来调用,会把类自动传入 放了一个view的内存地址(View--->as_view--->内层函数) # 请求来了,如果路径匹配,会执行,函数内存地址(request) def view(request, *args, **kwargs): # request是当次请求的request self = cls(**initkwargs) # 实例化得到一个对象,Book对象 if hasattr(self, 'get') and not hasattr(self, 'head'): self.head = self.get self.setup(request, *args, **kwargs) if not hasattr(self, 'request'): raise AttributeError( "%s instance has no 'request' attribute. Did you override " "setup() and forget to call super()?" % cls.__name__ ) return self.dispatch(request, *args, **kwargs) def dispatch(self, request, *args, **kwargs): # request是当次请求的request self是book对象 if request.method.lower() in self.http_method_names: # handler现在是 # handler = getattr(self,'get') 你写的Book类的get方法的内存地址 handler = getattr(self, request.method.lower(), self.http_method_not_allowed) else: handler = self.http_method_not_allowed return handler(request, *args, **kwargs) # 执行get(request)