53.Django CBV基类View源码解析

前述章节《Django的FBV与CBV模式》中我们讲解了 Django 中编写视图层函数的两种方式,一种是基于函数即 FBV,另外一种是 CBV 即基于类的视图函数。在本节,我们对类视图中所继承的 View 源码进一步分析,帮助大家更好的理解类视图。若以后在项目中使用它就会更加得心应手。

View 定义于 django/views/generic/base.py 文件中,其功能实现主要依赖于三个重要的方法分别如下所示:

  • dispatch
  • as_view
  • http_method_not_allowed

下面我们根据源码依次对这个三个方法进行分析。

1. http_method_not_allowed方法

这个方法返回 HttpResponseNotAllowed(405)响应,属于继承 HttpResponse 的子类,表示当前的请求类型不被支持。它在 View 类中的源码如下所示:

  1. class View:
  2. def http_method_not_allowed(self, request, *args, **kwargs):
  3. logger.warning(
  4. 'Method Not Allowed (%s): %s', request.method, request.path,
  5. extra={'status_code': 405, 'request': request}
  6. )
  7. return HttpResponseNotAllowed(self._allowed_methods())

比如当一个类视图函数之定义了 get 方法,而没有定义 post 方法,那么这个类视图函数接收到 post 的请求的时候,由于找不到相对应的 post 的定义,就会通过另一个方法 dispatch 分发到 http_method_not_allowed 方法中。

2. dispatch方法分析

dispatch 方法根据 HTTP 请求类型调用 View 中的同名函数,实现了请求的分发,源码如下所示:

  1. class View:
  2. def dispatch(self, request, *args, **kwargs):
  3. if request.method.lower() in self.http_method_names:
  4. handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
  5. else:
  6. handler = self.http_method_not_allowed
  7. return handler(request, *args, **kwargs)

http_method_names 定义当前 View可以接受的请求类型:

  1. http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']

首先,if 判断当前的请求类型是否可以被接受,即请求方是否定义在 http_method_names 变量中。若能够接受请求,则 dispatch() 尝试获取 View 中的同名方法,如果不存在,则将 handler 指定为 http_method_not_allowed 即不被允许的方法。然后 else 规定如果方法不被接受则直接将其指定为 http_method_not_allowed。

3. as_view方法分析

 Django 给 as_view 方法加了@classonlymethod装饰器,作用是只允许类对象调用这个方法,如果是类实例调用,将会抛出 AttributeError 异常。该方法的源码如下所示:

  1. class View:
  2. @classonlymethod
  3. def as_view(cls, **initkwargs):
  4. for key in initkwargs:
  5. if key in cls.http_method_names:
  6. raise TypeError("You tried to pass in the %s method name as a "
  7. "keyword argument to %s(). Don't do that."
  8. % (key, cls.__name__))
  9. if not hasattr(cls, key):
  10. raise TypeError("%s() received an invalid keyword %r. as_view "
  11. "only accepts arguments that are already "
  12. "attributes of the class." % (cls.__name__, key))
  13.  
  14. def view(request, *args, **kwargs):
  15. #创建View类实例
  16. self = cls(**initkwargs)
  17. if hasattr(self, 'get') and not hasattr(self, 'head'):
  18. self.head = self.get
  19. self.setup(request, *args, **kwargs)
  20. if not hasattr(self, 'request'):
  21. raise AttributeError(
  22. "%s instance has no 'request' attribute. Did you override "
  23. "setup() and forget to call super()?" % cls.__name__
  24. )
  25. #调用View实例的dispatch方法
  26. return self.dispatch(request, *args, **kwargs)
  27. view.view_class = cls
  28. view.view_initkwargs = initkwargs
  29. update_wrapper(view, cls, updated=())
  30. update_wrapper(view, cls.dispatch, assigned=())
  31. return view

Django 将一个 HTTP 请求映射到一个可调用的函数,而不是一个类对象。所以,在定义 URL 路由的时候总是需要调用 View 的 as_view 方法。可以看到,上述的as_view 方法,它创建了 View 类的实例,然后调用了 dispatch 方法,根据请求类型分发处理请求函数。

在本节我们通过介绍 View 的源码,详细解析了基于类视图的实现原理,当一个请求进来的时候,视图层是如何让处理的,分别调用了哪些方法,最终实现了请求的分发处理。

posted @ 2022-08-02 15:56  随遇而安==  阅读(41)  评论(0编辑  收藏  举报