Django中CBV及其源码解释

FBV(function base views) 就是在视图里使用函数处理请求。

CBV(class base views) 就是在视图里使用类处理请求。

Python是一个面向对象的编程语言,如果只用函数来开发,有很多面向对象的优点就错失了(继承、封装、多态)。所以Django在后来加入了Class-Based-View。可以让我们用类写View。这样做的优点主要下面两种:

  1. 提高了代码的复用性,可以使用面向对象的技术,比如Mixin(多继承)
  2. 可以用不同的函数针对不同的HTTP方法处理,而不是通过很多if判断,提高代码可读性

CBV源码解析:

首先,在urls中,我们会有这样一个访问函数:

path('login/',views.LoginView.as_view() )

  当访问login时,django会调用  views.LoginView.as_view() 中 as_view() 这个函数,那么必须先要去看看LoginView中该函数构成:

class LoginView(View):
def get(self,request): return render(request,"login.html")
def post(self,request): return HttpResponse("Hello")

  LoginView 是我们在视图中创建的类,继承View,LoginView 没有as_view这个方法,于是我们来到继承的父类View,并找到as_view

  @classonlymethod
def as_view(cls, **initkwargs):
"""Main entry point for a request-response process."""
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key)) def view(request, *args, **kwargs):
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
view.view_class = cls
view.view_initkwargs = initkwargs # take name and docstring from class
update_wrapper(view, cls, updated=()) # and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
return view

从源码中可以看到as_view最后返回了一个view

此时

path('login/',views.LoginView.as_view() )    ------->>>>   path('login/',View.view ) 

当有用户登录Login页面时,dajango会自动调用 View.view

又找到view的方法,源码如下:
  def view(request, *args, **kwargs):
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
解读一下view方法:
self = cls(**initkwargs)  cls为调用as_view时传入的类,为调用该类的对象 LoginView ,这里self为 LoginView 的实例对象.
return self.dispatch(request, *args, **kwargs)  最后返回的时候,先调用了self.dispatch(request, *args, **kwargs)这个方法.而cls就是LoginView ,一步步找到
dispatch这个方法
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.
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
return handler(request, *args, **kwargs)
if request.method.lower() in self.http_method_names:
   handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
其中
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
获取反射获取到对应的方法('get', 'post', 'put'....)
最后返回:handler(request, *args, **kwargs) 的调用结果,完成分发
 self.http_method_not_allowed 为错误或找不到值时报错调用的方法
更多可以查看

上一篇:Fresco 源码分析 —— 整体架构


下一篇:如何使用网盘托管git项目