$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自己的方法)--->分发到不同的函数,执#行函数,拿到结果
随机推荐
- 使用idea创建springboot项目并打成war包发布到tomcat8上
1.将pom.xml中的打包方式修改为war <groupId>com.borya</groupId> <artifactId>Project</artifa ...
- termios结构体的内容
一.结构体成员 struct termios { tcflag_t c_iflag; tcflag_t c_oflag; tcflag_t c_cflag; tcflag_t c_lflag; cc_ ...
- ****** 四十 ******、软设笔记【网络基础】-Internet和Intranet基础
Internet和Intranet基础 一.网络地址及子网掩码 1.IP地址结构及类别 IP地址是由32位二进制数,即4个字节组成的,由网络号和主机号两个字段组成. 网络号的位数决定了可以分配的网络数 ...
- linux 添加并格式化新硬盘
linux挂载磁盘1. 格式化磁盘mkfs -t ext4 /dev/sdb2. 挂载mount /dev/xvdb /home3. 查看磁盘对应的uuidblkid4. 加入开机自动挂载,在/etc ...
- 第25月第8天 100-Days-Of-ML-Code
1.100-Days-Of-ML-Code https://github.com/Avik-Jain/100-Days-Of-ML-Code https://github.com/llSourcell ...
- webpack 配置全局 jQuery 对象
将 lodash 添加到当前模块的上下文中 import _ from 'lodash' 但是你想每个模块都引入的话就特别麻烦,这里有插件可以帮助到您,只需在 webpack.config.js 中配 ...
- netty的解码器和粘包拆包
Tcp是一个流的协议,一个完整的包可能会被Tcp拆成多个包进行发送,也可能把一个小的包封装成一个大的数据包发送,这就是所谓的粘包和拆包问题 粘包.拆包出现的原因: 在流传输中出现,UDP不会出现粘包, ...
- MeEclipse搭建SSH框架之———大体框架
必备:MyEclipse软件,SSH需要的jar包,数据库,连接数据库的驱动jar包 先搭建大体框架,再加别的东西,要不都不知道哪里错了 一.新建web项目 MyEclipse左边右键--->N ...
- 02、natapp的使用
使用方法 官网: https://natapp.cn 命令启动:natapp -authtoken=***************** 文档:https://natapp.cn/article/nat ...
- Shiro入门 - 通过ini文件进行授权
shiro-permission.ini #用户 [users] #admin的密码是111111,此用户具有role1.role2两个角色 admin=111111,role1,role2 zhan ...