Django之CBV视图源码分析(工作原理)

1.首先我们先在urls.py定义CBV的路由匹配。

FBV的路由匹配:

2.然后,在views.py创建一名为MyReg的类:

注意:该类必须继续View类,且方法名必须与请求方式相同(后面会详解)

3.回到第一步的路由匹配可以看到MyReg.as_view(),直接调用了as_view函数。那么现在进去as_view函数看看里面运行了什么?

4.由上面的分析可以知道执行as_view函数时,返回该函数下嵌套的一个叫view函数的内存地址,这样,urls.py里的url(r'^my_reg/', views.MyReg.as_view())就相当于url(r'^my_reg/', views.view)了,这样跟我们之前的FBV就没区别了,当url匹配成功,就会执行view函数。

5.假设url匹配成功,执行view函数:

首先view函数完成了MyReg类的初始化

最后view函数 先调用dispatch函数, 然后返回dispatch函数执行后返回值,

6.现在进入dispatch函数看看它返回了什么数据

dispatch函数为CBV最精髓的部分

分析:

request.method.lower()为请求方式的小写

self.http_method_names点进去为一个列表,该列表是所有八个请求方式的小写

self.http_method_not_allowed返回一个报错信息为405的函数

getattr是反射,通过字符串获取方法的内存地址。拿到内存地址可以直接加()调用

最后总分析 dispatch函数

    def dispatch(self, request, *args, **kwargs):
        # Try to dispatch to the right method; if a method doesn't exist,
        # defer to the error handler. Also defer to the error handler if the
        # request method isn't on the approved list.
        # 判断用户发送的请求方式是否在http_method_names列表里面
        if request.method.lower() in self.http_method_names:  
            # 通过反射判断MyReg类是否有跟请求方式同名的方法,若有,返回该方法的内存地址,没有返回报错405
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
        else:
            # 如果不在就返回一个报错405的函数
            handler = self.http_method_not_allowed
        # 最后直接执行上面返回的函数
        return handler(request, *args, **kwargs)
posted @ 2019-10-28 18:51  tomjoy  阅读(423)  评论(0编辑  收藏  举报