$Django cbv源码分析 djangorestframework框架之APIView源码分析
1 CBV的源码分析
#视图
class login (View):
pass
#路由
url(r'^books/$', views.login.as_view())
#阅读源码:
#左侧工程栏--->设置图标-->点击--->show members(能看到py文件,pu下的类,类下的方法)
-Class Base View(基于类的视图)
-Function Base View(基于函数的视图)
-def as_view 类方法 :返回view
-def view:as_view的内的函数(闭包)
-python中一切皆对象:函数也是对象
-hasattr(self, 'get')--判断self类中是不是有该(get)方法
-setattr(self,get,get_all):相当于把get函数,变成了get_all
-getattr(self, 'get'):拿到get函数的内存地址
- 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
#执行:dispatch:谁的dispatch方法?写的cbv的那个c,视图中的那个视图类
#我这个类如果没有写dispatch,会执行View中的dispatch方法
return self.dispatch(request, *args, **kwargs)
-def dispatch(self, request, *args, **kwargs):
#request.method 前台请求的方法,转成了小写
#http_method_names View中定义的一个列表:是一堆请求方式
if request.method.lower() in self.http_method_names:
#getattr的第三个参数是默认值:self.http_method_not_allowed
#拿到get方法的内存地址
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
#get(request,*args, **kwargs)
return handler(request, *args, **kwargs
总结:*******请求来了--->as_view---->view---->dispatch--->分发到不同的函数,执#行函数,拿到结果
2 djangorestframework框架
安装:djangorestframework
-它是一个app,要在咱的项目中用
-只是快速的构建resful规范的接口
-csrf_exempt:局部禁用csrf(csrf是可以局部使用,局部禁用)
-以后再执行的dispatch方法是APIView的dispatch方法
-getattr和setattr
重点掌握这三点:
-request.data 是个方法,包装成了属性,前台传过来body体中数据的数据,放在里面
-request.query_params 这个是原来GET中的数据
-request把原来的request包装进去了
3 APIView源码分析
@classmethod
def as_view(cls, **initkwargs):
"""
Store the original class on the view function. This allows us to discover information about the view when we do URL
reverse lookups. Used for breadcrumb generation.
"""
if isinstance(getattr(cls, 'queryset', None), models.query.QuerySet):
def force_evaluation():
raise RuntimeError(
'Do not evaluate the `.queryset` attribute directly, '
'as the result will be cached and reused between requests. '
'Use `.all()` or call `.get_queryset()` instead.'
)
cls.queryset._fetch_all = force_evaluation view = super(APIView, cls).as_view(**initkwargs)
view.cls = cls
view.initkwargs = initkwargs # Note: session based authentication is explicitly CSRF validated,
# all other authentication is CSRF exempt.
return csrf_exempt(view)
as_view方法
def dispatch(self, request, *args, **kwargs):
"""
`.dispatch()` is pretty much the same as Django's regular dispatch,
but with extra hooks for startup, finalize, and exception handling.
"""
self.args = args
self.kwargs = kwargs
request = self.initialize_request(request, *args, **kwargs)
self.request = request
self.headers = self.default_response_headers # deprecate? try:
self.initial(request, *args, **kwargs) # Get the appropriate handler method
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 response = handler(request, *args, **kwargs) except Exception as exc:
response = self.handle_exception(exc) self.response = self.finalize_response(request, response, *args, **kwargs)
return self.response
dispatch
def initialize_request(self, request, *args, **kwargs):
"""
Returns the initial request object.
"""
parser_context = self.get_parser_context(request) return Request(
request,
parsers=self.get_parsers(),
authenticators=self.get_authenticators(),
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)
initialize_request
def initial(self, request, *args, **kwargs):
"""
Runs anything that needs to occur prior to calling the method handler.
"""
self.format_kwarg = self.get_format_suffix(**kwargs) # Perform content negotiation and store the accepted info on the request
neg = self.perform_content_negotiation(request)
request.accepted_renderer, request.accepted_media_type = neg # Determine the API version, if versioning is in use.
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme # Ensure that the incoming request is permitted
self.perform_authentication(request)
self.check_permissions(request)
self.check_throttles(request)
initial方法(内部调用认证,权限,频率)
总结:*******请求来了--->as_view---->view---->dispatch(apiview的比上面的多做了 包了个request对象生成新的request对象 和调用initial方法 都是apiview自己的方法)--->分发到不同的函数,执#行函数,拿到结果
随机推荐
- Spark MLlib 机器学习
本章导读 机器学习(machine learning, ML)是一门涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多领域的交叉学科.ML专注于研究计算机模拟或实现人类的学习行为,以获取新知识.新 ...
- python-虚拟环境搭建
虚拟环境 需求: --公司之有一台服务器 -目前运行这一个5年前开发的Django项目,基于1.5 -现在要基于Django2.0开发一套程序 ...
- 解析ArcGis拓扑——根据拓扑错误记录提取shp文件、导出Excel表格
在ArcGis拓扑检查的流程——以面重叠检查为例中讲述了如何在ArcGis进行拓扑检查与修改. 在实际操作中,有时我们还需要将ArcGis拓扑检查的结果制作成报告或者提取错误信息反馈作业方. 本文仍然 ...
- python中执行py文件出错(提示File “<stdin>”,line 1,SyntaxError:invalid syntax)
解决办法: 上图中已通过输入python进入了python运行环境,出现>>>时候的不能再用python z.py 来运行hello.py文件: 应该通过exit()退出当前pyth ...
- echarts 导出图片,并将图片导出pdf格式
1.官方下载echarts 包. 2.实例案例: 1)页面: <h2>Index</h2> <div id="main" style="he ...
- cocos2dx 动画控制概要
-------------------------------------------------Cocos2d 播放动画Node : 节点,所有显示容器的基础 Sprite : 使用图片的节点 An ...
- [Ynoi2016]这是我自己的发明 莫队
传送门:here 很棒的莫队题啊..... 题意: 有一棵$ n$个点的树,树上每个点有点权,有$ m$次询问: 操作1:给定两个点$ x,y$,求二元组$ (a,b)$的数量,要求$ a$在$ x$ ...
- python3:实现输出等边三角形、直角三角形
学习python,用的是3.5的版本: 记录一下学习历程~ 一.先来一个简单的,输出直角三角形: ***知识点:for循环,range()函数 二.进阶:输出等边三角形 ******知识点:嵌套for ...
- 库zlog的使用手册
库官方网址: 使用手册: http://hardysimpson.github.io/zlog/UsersGuide-CN.html#htoc11 [formats] simple = &quo ...
- 【深入分析Java Web技术内幕】2、深入分析Java I/O的工作机制
Java的I/O类库的基本架构 基于字节操作的IO接口:InputStream.OutputStream 基于字符操作的IO接口:Writer.Reader 基于磁盘操作的IO接口:File 基于网络 ...