• CBV源码分析


    # 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)
  • 相关阅读:
    动态类(类工厂)总结
    m3u8字段意义解析
    libevent+bufferevent总结
    g++编译总结
    ffplay 参数说明分享
    PHP——最新号码归属地数据库
    PHP——汉字完美转为ASCII码
    PHP——json_encode转码保留中文
    workerman——报错
    PHP——封装Curl请求方法支持POST | DELETE | GET | PUT 等
  • 原文地址:https://www.cnblogs.com/ZhZhang12138/p/14875752.html
Copyright © 2020-2023  润新知