首页 技术 正文
技术 2022年11月23日
0 收藏 735 点赞 4,337 浏览 2620 个字

目录

解析器

(1)api/urls.py

# api/urls.pyfrom django.urls import path,re_path
from .views import UserView,PaserViewurlpatterns = [
re_path('(?P<version>[v1|v2]+)/users/', UserView.as_view(),name = 'api_user'),
path('paser/', PaserView.as_view(),), #解析
]

(2)views.py

from rest_framework.parsers import JSONParser,FormParserclass PaserView(APIView):    parser_classes = [JSONParser,FormParser,]
#JSONParser:表示只能解析content-type:application/json的头
#FormParser:表示只能解析content-type:application/x-www-form-urlencoded的头 def post(self,request,*args,**kwargs):
#获取解析后的结果
print(request.data)
return HttpResponse('paser')

(3)通过postman发送Json数据

Django rest framework(5)—-解析器

在后台可以获取发过来的Json数据

Django rest framework(5)—-解析器

源码流程

(1)dispatch

    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进行加工,丰富了一些功能
#Request(
# request,
# parsers=self.get_parsers(),
# authenticators=self.get_authenticators(),
# negotiator=self.get_content_negotiator(),
# parser_context=parser_context
# )
#request(原始request,[BasicAuthentications对象,])
#获取原生request,request._request
#获取认证类的对象,request.authticators
#1.封装request
request = self.initialize_request(request, *args, **kwargs)
self.request = request
self.headers = self.default_response_headers # deprecate? try:
#2.认证
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

(2)initialize_request

获取所有解析器

 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(), #[BasicAuthentication(),],把所有的认证类对象封装到request里面了
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)

(3)get_parsers

    def get_parsers(self):
"""
Instantiates and returns the list of parsers that this view can use.
"""
return [parser() for parser in self.parser_classes]

(4)parser_classes

Django rest framework(5)—-解析器

同样我们可以在settings里面全局配置

#全局配置
REST_FRAMEWORK = {
#版本
"DEFAULT_VERSIONING_CLASS":"rest_framework.versioning.URLPathVersioning",
#解析器
"DEFAULT_PARSER_CLASSES":["rest_framework.parsers.JSONParser","rest_framework.parsers.FormParser"]
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838