Django rest framework(5)----解析器
解析器
(1)api/urls.py
1 2 3 4 5 6 7 8 9 | # api/urls.py from django.urls import path,re_path from .views import UserView,PaserView urlpatterns = [ re_path( '(?P<version>[v1|v2]+)/users/' , UserView.as_view(),name = 'api_user' ), path( 'paser/' , PaserView.as_view()), #解析 ] |
(2)views.py
1 2 3 4 5 6 7 8 9 10 11 12 | from rest_framework.parsers import JSONParser,FormParser class 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数据
在后台可以获取发过来的Json数据
源码流程
(1)dispatch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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
获取所有解析器
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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
1 2 3 4 5 | 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
同样我们可以在settings里面全局配置
1 2 3 4 5 6 7 | #全局配置 REST_FRAMEWORK = { #版本 "DEFAULT_VERSIONING_CLASS" : "rest_framework.versioning.URLPathVersioning" , #解析器 "DEFAULT_PARSER_CLASSES" :[ "rest_framework.parsers.JSONParser" , "rest_framework.parsers.FormParser" ] } |
这个解析器好简单啊
生活不会突变,你要做的只是耐心和积累。人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。你的时间有限,做喜欢的事情会令人愉悦,所以跟随自己的本心。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步