CBV和APIView源码分析
CBV源码分析
查看源码的方式,先查看自身,没有去找父类,父类没有就去找父父类。。。

自己定义的类
class Author(View):
def get(self,request):
back_dic = {'status_code': 100, 'msg': 'get ok'}
return JsonResponse(back_dic) def post(self,request):
back_dic = {'status_code': 102, 'msg': 'post ok'}
return JsonResponse(back_dic)
我们看到自己定义的类中并没有这个as_view()方法,那么他只能是从我们继承的父类View中来的,那我们就点到View里面去看有没有as_view()这个方法

刚好就在这个里面,然后点开这个方法,看里面具体做了什么操作
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
CBV as_view()源码
简单分析,如图

我们看到,调用了as_view()实际上是走了view方法,然后再view方法中给我们返回了一个对象的dispatch方法,那么我们就得搞清楚,现在的self是谁~
self是View的对象,然后我们自定义的类继承了View,所以说self就是我们自己定义类的对象,那么就简单了,我们先去自己定义的类中找我们有没有写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)
源码分析

此时的handler就是我们发送的请求方式,比如GET,但是是小写的而已,然后再自己定义的类中与get对应上了,所以就会get请求走get,post请求走post。
此处的精髓通过反射取到请求方式
CBV和APIView源码分析的更多相关文章
- cbv+resful+APIView源码分析
CBV源码分析 1概念:什么是cbv和fbv 已经什么是API class bass View ---基于类的视图 function bass View ---基于函数的视图 API(Applicat ...
- CBV源码分析+APIVIew源码分析
{drf,resful,apiview,序列化组件,视图组件,认证组件,权限组件,频率组件,解析器,分页器,响应器,URL控制器,版本控制} 一.CBV源码分析准备工作: 新建一个Django项目 写 ...
- $Django cbv源码分析 djangorestframework框架之APIView源码分析
1 CBV的源码分析 #视图 class login (View): pass #路由 url(r'^books/$', views.login.as_view()) #阅读源码: #左侧工程栏--- ...
- Restful规范-APIView源码分析
目录 一.Restful规范 十条规范 二.drf的简单使用 三.APIView源码分析 CBV源码分析 APIView源码分析 一.Restful规范 Restful规范是一种web API接口的设 ...
- Django rest framework框架——APIview源码分析
一.什么是rest REST其实是一种组织Web服务的架构,而并不是我们想象的那样是实现Web服务的一种新的技术,更没有要求一定要使用HTTP.其目标是为了创建具有良好扩展性的分布式系统. 可用一句话 ...
- drf的基本使用、APIView源码分析和CBV源码拓展
cbv源码拓展 扩展,如果我在Book视图类中重写dispatch方法 -可以实现,在get,post方法执行之前或者之后执行代码,完成类似装饰器的效果 def dispatch(self, requ ...
- django Rest Framework----APIView 执行流程 APIView 源码分析
在django—CBV源码分析中,我们是分析的from django.views import View下的执行流程,这篇博客我们介绍django Rest Framework下的APIView的源码 ...
- 探索drf执行流程之APIView源码分析
Django REST framework 简介 现在新一代web应用都开始采用前后端分离的方式来进行,淘汰了以前的服务器端渲染的方式.而实现前后端分离是通过Django REST framework ...
- DRF中的APIView源码分析
首先写一个简单的drf接口 from rest_framework.views import APIView from rest_framework.response import Response ...
随机推荐
- 事务Transaction
目录 为什么写这系列的文章 事务概念 ACID 并发事务导致的问题 脏读(Dirty Read) 非重复读(Nonrepeatable Read) 幻读(Phantom Reads) 丢失修改(Los ...
- APScheduler使用总结
安装 pip install apscheduler APScheduler组件 1.triggers(触发器) 触发器中包含调度逻辑,每个作业都由自己的触发器来决定下次运行时间.除了他们自己初始配置 ...
- 续python学习(一)
接上面没写完的知识点继写. 当然,这些知识点都很简单,可能没必要花费太多时间去记忆,多写写代码就会了. 5.字符串的使用.索引和切片是字符串主要的两个应用.索引:顾名思义就是找出某个字符在一个字符串中 ...
- 学以致用:手把手教你撸一个工具库并打包发布,顺便解决JS浮点数计算精度问题
本文讲解的是怎么实现一个工具库并打包发布到npm给大家使用.本文实现的工具是一个分数计算器,大家考虑如下情况: \[ \sqrt{(((\frac{1}{3}+3.5)*\frac{2}{9}-\fr ...
- Linux命令之解压缩命令tar,zip,rar
一.tar命令 1.压缩命令 1)压缩为.tar格式 tar -cvf destination.tar source 2)压缩为.tar.gz格式 tar -cvf destination.tar.g ...
- HTC推出了VIVE Comos 全新 VR(虚拟现实)系列产品
据 The Verge 报道,近日,HTC 推出了 VIVE Comos 全新 VR(虚拟现实)系列产品.包括 Cosmos 精英套装.VIVE Cosmos XR 版.Cosmos Play 基础版 ...
- Ansible-1 基本认识及清单与模块
ansible 一.常用的自动化运维工具 1.puppet 基于ruby开发,采用c/s架构,扩展性强,基于ssl,远程命令执行相对较弱, 2.saltstack 基于python开发,采用C/S架构 ...
- requests.exceptions.SSLError报错
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries excee ...
- eslint常用三种校验语句
1.关闭对整段代码的校验 /* eslint-disable */ code /* eslint-enable */ 2.关闭当前行代码的校验 line code // eslint-disable- ...
- Serverless与微服务
Serverless 是一个更大的范畴,Serverless 不只计算,也包括存储.数据库.中间件等各种服务.Serverless = FaaS(函数即服务) + BaaS(后端即服务).其中 Ser ...