APIView中的dispatch
(1)dispatch方法详解----封装原有的request对象
(原request中的方法和属性均可直接在封装后的request中调用,或者使用request._request也可,如:request.user == request._request.user
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
#1.对原来的request进行进一步封装
request = self.initialize_request(request, *args, **kwargs)
self.request = request#(2)request已经是经过进一步封装的
self.headers = self.default_response_headers # deprecate? try:
#2.增加对request的调用
self.initial(request, *args, **kwargs)#(3)比View中多的执行的方法,使用封装过后的request进行调用 # 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)#(4)对原响应对象进行进一步封装
return self.response
dispatch方法重写
(2)initialize_request对原生request进行封装
def initialize_request(self, request, *args, **kwargs):
"""
Returns the initial request object.
"""
parser_context = self.get_parser_context(request) return Request(
request,#(1-1)原来的request
parsers=self.get_parsers(),#(1-2)[反序列化方式列表]
authenticators=self.get_authenticators(),#(1-3)实例化调用了rest_framework配置文件[中间件的认证类列表]---[BasicAuthentication对象,SessionAuthentication对象]
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)
initialize_request封装request
(3)self.initial详解----认证+权限+节流+版本控制
self.initial(request, *args, **kwargs)#(3)比View中多的执行的方法,使用封装过后的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.(3-0)api版本的获取
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)#(3-1)进行认证是否登录
self.check_permissions(request)#(3-2)权限组件
self.check_throttles(request)#频率组件
self.initial详解
APIView中的dispatch的更多相关文章
- CBV中的dispatch
之前介绍了FBV和CBV ,下面我们看一下CBV中的dispatch dispatch函数在类View中定义,作用就是通过反射查找get或post函数,所以在执行get或post函数之前,dispat ...
- GCD 中使用 dispatch group 进行同步操作
话不多说,先上代码,在分析 Code - (void)viewDidLoad { [super viewDidLoad]; dispatch_group_t group1 = dispatch_gro ...
- 深入ObjC GCD中的dispatch group工作原理。
本文是基于GCD的支持库libdispatch的源代码分析的结果或是用于作为源代码阅读的参考,尽量不帖代码,力求用UML图来说明工作流. 本文参考的源代码版本为v501.20.1,如有兴趣请自行到苹果 ...
- vuex中的dispatch和commit
dispatch:含有异步操作,eg:向后台提交数据,写法: this.$store.dispatch('mutations方法名',值) commit:同步操作,写法:this.$store.com ...
- DRF中的APIView源码分析
首先写一个简单的drf接口 from rest_framework.views import APIView from rest_framework.response import Response ...
- RestFramework——API基本实现及dispatch基本源码剖析
基于Django实现 在使用RestFramework之前我们先用Django自己实现以下API. API完全可以有我们基于Django自己开发,原理是给出一个接口(URL),前端向URL发送请求以获 ...
- django Rest Framework----APIView 执行流程 APIView 源码分析
在django—CBV源码分析中,我们是分析的from django.views import View下的执行流程,这篇博客我们介绍django Rest Framework下的APIView的源码 ...
- DRF(1) - REST、DRF(View源码解读、APIView源码解读)
一.REST 1.什么是编程? 数据结构和算法的结合. 2.什么是REST? 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下: /books/ /get_all_books/ 访问所 ...
- REST、DRF(View源码解读、APIView源码解读)
一 . REST 前言 1 . 编程 : 数据结构和算法的结合 .小程序如简单的计算器,我们输入初始数据,经过计算,得到最终的数据,这个过程中,初始数据和结果数据都是数据,而计算 ...
随机推荐
- koa+mysql实现增删改查-全栈之路(001)
Date: 2020-4-23 以前很少写文章,从今天开始我要挑战一下自己,连续输出100篇技术类文章.这100篇文章我尽量以实战案例为主. 如果你觉得本文还不错,记得关注或者给个 star,你们的赞 ...
- HTML后台管理页面布局
设计网页,让网页好看:网上找模板 搜 HTML模板 BootStrap 一.内容回顾: HTML 一大堆的标签:块级.行内 CSS position background text-align mar ...
- document.documentElement.scrollTop指定位置失效解决办法
近期在vue的H5项目中,做指定位置定位的时候发现使用document.documentElement.scrollTop一直不生效. 解决办法是document.documentElement.sc ...
- Socket中SO_REUSEADDR简介
SO_REUSEADDR:字面意思重复使用地址 一般来说,一个端口释放后会等待两分钟之后才能再次被使用,SO_REUSEADDR是让端口释放后立即就可以被再次使用. SO_REUSEADDR用于对TC ...
- python(For 循环语句)
一.For循环 Python for 循环可以遍历任何序列的项目,如一个列表或者一个字符串或者字典等. 语法模式:for var in sequence: (1)从某个集合(列表等)里顺次取值 #遍历 ...
- Cell Phone Network G
最小点队的题意:https://www.luogu.com.cn/problem/P2899 与战略游戏不同的是,这里要求占领所有的点而不是边. 1自己被自己染色(有信号塔) 这时我们可以想一下,u被 ...
- springmvc与struts2执行流程比较
之前写过一篇struts2的执行流程的文章了,这里对struts2的流程就不做过多的分析,这篇文章主要分析spring-mvc的执行流程以 及struts2与spring-mvc的差别. 1.stru ...
- c语言中的malloc函数
少壮不努力,大一的时候c语言学得不扎实,最近学数据结构的时候看到c语言中malloc函数都不知道了,这里记录一下,避免以后再忘. malloc的全称是memory allocation,中文叫动态内存 ...
- LeetCode--Unique Morse Code Words && Flipping an Image (Easy)
804. Unique Morse Code Words (Easy)# International Morse Code defines a standard encoding where each ...
- Coursera课程笔记----C程序设计进阶----Week 4
指针(一) (Week 4) 什么是"指针" 互联网上的资源--地址 当获得一个地址,就能得到该地址对应的资源,所以可以把"网址"称为指向资源的"指针 ...