django CBV 源码分析

FBV和CBV

FBV(function base views) 就是在视图里使用函数处理请求。 
在之前django的学习中,我们一直使用的是这种方式,所以不再赘述。

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

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

CBV 源码分析

class View(object):
"""
Intentionally simple parent class for all views. Only implements
dispatch-by-method and simple sanity checking.
""" http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'] def __init__(self, **kwargs):
"""
Constructor. Called in the URLconf; can contain helpful extra
keyword arguments, and other things.
"""
# Go through keyword arguments, and either save their values to our
# instance, or raise an error.
for key, value in six.iteritems(kwargs):
setattr(self, key, value) @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 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) def http_method_not_allowed(self, request, *args, **kwargs):
logger.warning(
'Method Not Allowed (%s): %s', request.method, request.path,
extra={'status_code': 405, 'request': request}
)
return http.HttpResponseNotAllowed(self._allowed_methods()) def options(self, request, *args, **kwargs):
"""
Handles responding to requests for the OPTIONS HTTP verb.
"""
response = http.HttpResponse()
response['Allow'] = ', '.join(self._allowed_methods())
response['Content-Length'] = ''
return response def _allowed_methods(self):
return [m.upper() for m in self.http_method_names if hasattr(self, m)]

class View(object)

在url中会执行一个叫做as_view的方法,这个方法在加载url时候会执行   得到一个返回值view 是一个函数 供url调用

那么view 究竟做了什么呢?

当url执行view时候,会把下边这句代码作为返回值

self.dispatch(request, *args, **kwargs)

所view 会调用当前调用类的dispatch

CBV 的api接口 dispatch

在执行dispatch的时候,如果views中的视图类 自定义了dispatch那么将优先执行自定义的dispatch  由此可在请求处理前增加一些处理,

然后再用super方法去调用父类的dispatch ,当然也可以自己把父类的功能全部写在自己的自定义里边。

那么就说下父类的dispatch做了什么功能呢

简单的说-----分发

复杂的说-----找到这次请求对应的类型,然后用反射把这个方法取到,然后作为自己的返回值,返回给调用这个方法的view

因此我们得到一个结论,两种方式的最终结果都是把要执行的视图函数放在url的第二个参数位置  不过CBV在请求的前后给我们提供了丰富的扩展空间

CBV流程的更多相关文章

  1. CBV请求流程源码分析

    一.CBV流程解析 urls.py urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^book/', views.BookView.as ...

  2. flask中的CBV和FBV

    flask中CBV使用 from flask import Flask, views app = Flask(__name__) class Login(views.MethodView): meth ...

  3. rest_framework 认证流程

    一.基本流程 rest_framework框架是基于CBV基础开发的(VPIView(View)),所以基本流程与CBV流程相似 当我们的请求发来后,会走as_views,执行view里面的方法,最开 ...

  4. 分页组件与CBV

    一. 自定义分页 1.准备工作 (1).首先在models.py中创建一张book表用来存储数据 from django.db import models class Book(models.Mode ...

  5. Django框架深入了解_01(Django请求生命周期、开发模式、cbv源码分析、restful规范、跨域、drf的安装及源码初识)

    一.Django请求生命周期: 前端发出请求到后端,通过Django处理.响应返回给前端相关结果的过程 先进入实现了wsgi协议的web服务器--->进入django中间件--->路由f分 ...

  6. ORM some

    1 -- 增 models.表名(类).objects.create(字段1=值,字段2=值) 查 models.表名(类).objects.get(pk = 3) models.表名(类).obje ...

  7. day 94 RestFramework序列化组件与视图view

    一 .复习 1. CBV流程 class BookView(View): def get(): pass def post(): pass #url(r'^books/', views.BookVie ...

  8. restframework框架之认证

    1. 认证之APIView 在聊APIView之前, 我们先重温一下django2.x的CBV流程 a. 对于django而言, 当浏览器请求到达之后,按照规则首先会经过各大中间件(Middlewar ...

  9. Django 内容回顾

    模板 变量 {{ }} 标签 {% %} if elif else for empty forloop() with...as csrf_token 过滤器 default length add da ...

随机推荐

  1. 如何解决nagios安装及运行在的常见错误?(转)

    一.安装nrpe的时候,提示:checking for SSL headers… configure: error: Cannot find ssl headers 解决方法如下: yum -y in ...

  2. springMVC中Restful支持

    RESTFul支持 http://localhost:8090/user/doAdd.action?username=tony&age=8 http://localhost:8090/user ...

  3. Wix 安装部署教程 -CustomAction的七种用法

    在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...

  4. C# 关键字 Visual Studio 2012

    C# 关键字 Visual Studio 2012 其他版本 关键字是对编译器具有特殊意义的预定义保留标识符. 它们不能在程序中用作标识符,除非它们有一个 @ 前缀. 例如,@if 是有效的标识符,但 ...

  5. driver: Linux设备模型之input子系统具体解释

    本节从总体上解说了输入子系统的框架结构.有助于读者从总体上认识linux的输入子系统.在陷入代码分析的过程中,通过本节的知识可以找准方向,明确原理. 本节重点: 输入子系统的框架结构 各层相应内核中的 ...

  6. DriverStore文件夹特别大,能删除吗?

    DriverStore文件夹特别大,能删除吗? DriverStore\FileRepository文件夹特别大,能删除吗? C:\Windows\System32\DriverStore\FileR ...

  7. linux 下 查看进程的启动时间和运行时间

    $ps -A -opid,stime,etime,args | grep switcher : : grep switcher Jan22 :: /home/a/search/switcher//bi ...

  8. hadoop english

    for the same 同样previously 之前地overlay v. 覆盖; 镀金variable expansion 变量替换processed for 处理 entry(entries) ...

  9. 初学spring(二)

      1.spring推荐使用接口编程,配合di可以达到层与层之间解耦

  10. Silverlight实例教程 - Validation数据验证DataAnnotation机制和调试技巧(转载)

    Silverlight 4 Validation验证实例系列 Silverlight实例教程 - Validation数据验证开篇 Silverlight实例教程 - Validation数据验证基础 ...