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 . 编程 : 数据结构和算法的结合 .小程序如简单的计算器,我们输入初始数据,经过计算,得到最终的数据,这个过程中,初始数据和结果数据都是数据,而计算 ...
随机推荐
- Django入门3:视图views
1.获取用户请求数据 1.1 request.GET 获取request.method='GET'的数据 request.GET.get('name',None) 1.2 request.POST 获 ...
- mac OS 安装 nvm
nvm官网 https://github.com/creationix/nvm nvm,node,npm之间的区别 nvm:nodejs 版本管理工具 一个 nvm 可以管理很多 node 版本和 n ...
- centos分配IP脚本--写的第一个shell脚本
IDC小菜鸟一枚,非科班出身.常常有客户的centos服务器需要分配15个IP甚至30个IP.每次需要手动分配十分麻烦,于是花了一天时间学了shell脚本,写了这个脚本. #!/bin/bash re ...
- Codeforce 140C (贪心+优先队列)补题
C. New Year Snowmen time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...
- 基于thinkphp3.2.3开发的CMS内容管理系统(二)- Rbac用户权限
基于thinkphp3.2.3开发的CMS内容管理系统 thinkphp版本:3.2.3 功能: --分类栏目管理 --文章管理 --商品管理 --用户管理 --角色管理 --权限管理 --友情链接管 ...
- shell之路 shell核心语法【第四篇】流程控制
if语句 if ... fi 语句: if ... else ... fi 语句: if ... elif ... else ... fi 语句. 注意: expression 和方括号([ ])之间 ...
- 首次使用AWS服务器EC2
AWS有一年的免费套餐,这个便宜我得占. 申请的时候需要填写银行卡,AWS暂不支持储蓄卡,只好绑信用卡了. 创建EC2实例之后,下一个要解决的问题就是远程root访问. 1. 修改安全组设置 2. s ...
- linux进程管理相关命令
ps ps aux ps -ef | grep -E "supervisor|PPID" top 可以按一定规则对top的结果进行排序 # 监控单一进程 top -p pid ...
- D. Almost All Divisors(数学分解因子)
其实这题并不难啊,但是分解因子的细节一定要小心. \(比如样例48,2是因子说明24也是因子,也就是说假如x存在\) \(那么x一定是因子中的最小数乘上最大数\) \(那我们现在去验证x是否存在,先拿 ...
- 【XR-3】核心城市(树直径)
[XR-3]核心城市 这题真的难啊......... k个核心城市太麻烦,我们假设先找一个核心城市,应该放在哪里? \(任意取一个点,它的最远端是直径的端点.\) \(所以当这个点是直径的中点时,可以 ...