FBV与CBV 及CBV源码分析
FBV与CBV 及CBV源码分析
FBV(Function Based View) 基于函数的视图
基于函数的视图,我们一直在用没啥好讲的,就是导入模块调用函数执行业务
CBV(Class Based View) 基于类的视图
路由
from app01 import views
url(r'^haha/',views.zx_view.as_view()),
视图
class zx_view(View):
def get(self,request):
return render(request,'edit.html')
def post(self,request):
return HttpResponse("你好我是POST")
CBV源码分析
首先我们的路由竟然是一个函数(),这样的话是直接执行的,我们找到返回值就行
url(r'^haha/',views.zx_view.as_view()),
进入源码
@classonlymethod
def as_view(cls, **initkwargs):
"""
Main entry point for a request-response process.
"""
#views.zx_view.as_view(),我们调用的时候并没有传递关键字参数,所以这个for可以跳过
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
路由就相当于是这个了
那么就和FBV一样了,路由触发函数的执行
url(r'^haha/',view),
触发路由查看view是怎么执行的
def view(request, *args, **kwargs):
#这个self是什么,就是我们之前创建的zx_view类对象,这里直接实例化了一个self对象
self = cls(**initkwargs)
#通过反射获取get方法
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
#给self对象添加属性
self.request = request
self.args = args
self.kwargs = kwargs
#最后执行self.dispatch(request, *args, **kwargs)
return self.dispatch(request, *args, **kwargs)
self.dispatch我们知道我们的zx_view是没有这个方法的,那么就去它的父类找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,http_method_names信息下面那段代码
if request.method.lower() in self.http_method_names:
#通过反射拿到zx_view的get或者post函数对象
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
#执行zx_view的get或者post函数对象,并返回结果,结束
return handler(request, *args, **kwargs)
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
FBV与CBV 及CBV源码分析的更多相关文章
- cbv+resful+APIView源码分析
CBV源码分析 1概念:什么是cbv和fbv 已经什么是API class bass View ---基于类的视图 function bass View ---基于函数的视图 API(Applicat ...
- CBV和APIView源码分析
CBV源码分析 查看源码的方式,先查看自身,没有去找父类,父类没有就去找父父类... 自己定义的类 class Author(View): def get(self,request): back_di ...
- CBV源码分析+APIVIew源码分析
{drf,resful,apiview,序列化组件,视图组件,认证组件,权限组件,频率组件,解析器,分页器,响应器,URL控制器,版本控制} 一.CBV源码分析准备工作: 新建一个Django项目 写 ...
- django CBV视图源码分析
典型FBV视图例子 url路由系统 from django.conf.urls import url from django.contrib import admin from luffycity.v ...
- Django day24 cbv和APIView的源码分析 和 resful的规范
一:cbv的源码分析 1.CBV和FBV的区别: - Class Base View CBV(基于类的视图) - Function Base View FBV(基于函数的视图) 2.as_vi ...
- Django框架(十七)-- CBV源码分析、restful规范、restframework框架
一.CBV源码分析 1.url层的使用CBV from app01 import views url(r'book/',views.Book.as_view) 2.as_view方法 as_view是 ...
- Django框架深入了解_01(Django请求生命周期、开发模式、cbv源码分析、restful规范、跨域、drf的安装及源码初识)
一.Django请求生命周期: 前端发出请求到后端,通过Django处理.响应返回给前端相关结果的过程 先进入实现了wsgi协议的web服务器--->进入django中间件--->路由f分 ...
- Django框架(十八)—— CBV源码分析、restful规范、restframework框架
目录 CBV源码分析.restful规范.restframework框架 一.CBV源码分析 1.url层的使用CBV 2.as_view方法 3.view方法 4.dispatch方法(可以在视图层 ...
- $Django cbv源码分析 djangorestframework框架之APIView源码分析
1 CBV的源码分析 #视图 class login (View): pass #路由 url(r'^books/$', views.login.as_view()) #阅读源码: #左侧工程栏--- ...
随机推荐
- 运营的Python指南 - Python 操作Excel
这是一份写给运营人员的Python指南.本文主要讲述如何使用Python操作Excel.完成Excel的创建,查询和修改操作. 相关代码请参考 https://github.com/RustFishe ...
- Ubuntu 10.04——boa服务器的搭建
声明:自从第一次发表博文不知不觉过去了好久了,非常抱歉没能把自己的东西分享出来,但是由于上家公司本月初裁员,所以致使学的新东西成了半成品,无奈又换了一家,目前已工作三周了,自己也很想写博文分享知识, ...
- 非旋treap (fhq treap) 指针版
传送门 看了一圈,好像真的没什么用指针的呢.. 明明觉得指针很好看(什么??你说RE???听不见听不见) 其实我觉得用数组的话不RE直接WA调起来不是更困难嘛,毕竟通过gdb还可以知道哪里RE,WA就 ...
- Redis过期--淘汰机制的解析和内存占用过高的解决方案
echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! Red ...
- day2 上午 游戏 对应关系--->判断素数---->多重背包 神题
#include<iostream> using namespace std; int n; ; ]; long long p[maxn]; long long dp[maxn][maxn ...
- Java Part 001( 01_01_Java概述 )
Java作为编程语言, 甚至超出了语言的范畴, 成为一种开发平台, 一种开发规范. Java语言相关的JavaEE规范里, 包含了时下最流行的各种软件工程理念, 学习Java相当于系统的学习了软件开发 ...
- python使用openpyxl操作excel总结
安装openpyxl pip install openpyxl 简单示例 from openpyxl import Workbook #创建一个工作薄对象,也就是创建一个excel文档 wb = Wo ...
- Look into Bitmap images
What's a Bitmap image? I'm not going to explain the differences between raster and vector images, no ...
- 张孝祥java高新技术 --- jkd1.5 新特性
1. 静态导入 import static java.lang.Math.max; 2. 可变参数 3. 自动装箱,拆箱 4. 枚举
- hdu 1863 畅通工程 (prim)
畅通工程Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...