DRF解析器
1. DRF解析器局部配置
在API开发中,
django中默认接收的POST请求数据解析一般放在request.POST中,但前提是请求有一定要求:
1.Content_type:application/x-www-form-urlencoded (表单或ajax)或 multipart/form-data(表单文件)
2.数据格式为:k1 = v1 & k2 = v2
但在实际开发中,API通常是以JSON格式传输,所以需要在做特殊处理,在rest_framework的parsers模块中封装了多种解析器可供使用:
解析结果都放在封装之后的request.data中(原有的request取值方式仍可用,只有在调用request.data是才会查找指定解释器进行解析):
JSONParser:解析json格式----'application/json'
FormParser:解析form提交----'application/x-www-form-urlencoded'
MultiPartParser:解析form文件上传----'multipart/form-data'
FileUploadParser解析所有格式----'*/*'
urls.py
drf主路由配置urls.py:
from django.conf.urls import url,include urlpatterns = [ url(r'^api/', include('api.urls')), ]
api主路由配置urls.py:
from django.conf.urls import url from api import views #http://127.0.0.1:8000/api/v1/users/ urlpatterns = [ url(r'^(?P<version>v[\d+])/users/',views.UsersView.as_view(),name='users'), url(r'^(?P<version>v[\d+])/parsers/', views.ParsersView.as_view(), name='parsers'), ]
views.py:
from django.shortcuts import render, HttpResponse from rest_framework.views import APIView from rest_framework.parsers import JSONParser,FormParser,MultiPartParser,FileUploadParser class ParsersView(APIView): #解析器组件(局部配置)----不配置默认全都支持 parser_classes = [FormParser] ''' from rest_framework.parsers import JSONParser,FormParser,MultiPartParser,FileUploadParser JSONParser:'application/json' FormParser:'application/x-www-form-urlencoded' MultiPartParser:'multipart/form-data' FileUploadParser:'*/*' ''' def post(self,request,*args,**kwargs): print(request.POST) #只有在获取请数据时采取解析执行 print(request.data) return HttpResponse('DRF解析器测试')
2. DRF解析器全局配置
DRF解析器在不指定时默认支持所有格式解析
settings.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'api.apps.ApiConfig', 'rest_framework', ] REST_FRAMEWORK = { #版本组件(全局配置)----在认证+权限+节流之前 'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.URLPathVersioning',#设置版本组件类 'DEFAULT_VERSION':'v1',#默认的API版本 'ALLOWED_VERSIONS':['v1','v2'],#允许的API版本 'VERSION_PARAM':'version',#版本的key,默认的就是version(注意要和api接口中的key对应) #解析器组件 'DEFAULT_PARSER_CLASSES':['rest_framework.parsers.JSONParser','rest_framework.parsers.FormParser',],#可指定多个解析器,不配置默认支持所有 }
urls.py
drf主路由配置urls.py:
from django.conf.urls import url,include urlpatterns = [ url(r'^api/', include('api.urls')), ]
api主路由配置urls.py:
from django.conf.urls import url from api import views #http://127.0.0.1:8000/api/v1/users/ urlpatterns = [ url(r'^(?P<version>v[\d+])/users/',views.UsersView.as_view(),name='users'), url(r'^(?P<version>v[\d+])/parsers/', views.ParsersView.as_view(), name='parsers'), ]
views.py:
from django.shortcuts import render, HttpResponse from rest_framework.views import APIView from rest_framework.parsers import JSONParser,FormParser,MultiPartParser,FileUploadParser class ParsersView(APIView): def post(self,request,*args,**kwargs): print(request.POST) #只有在获取请数据时采取解析执行 print(request.data) return HttpResponse('DRF解析器测试')
在API开发中,
django中默认接收的POST请求数据解析一般放在request.POST中,但前提是请求有一定要求:
1.Content_type:application/x-www-form-urlencoded (表单或ajax)或 multipart/form-data(表单文件)
2.数据格式为:k1 = v1 & k2 = v2
但在实际开发中,API通常是以JSON格式传输,所以需要在做特殊处理,在rest_framework的parsers模块中封装了多种解析器可供使用:
解析结果都放在封装之后的request.data中(原有的request取值方式仍可用,只有在调用request.data是才会查找指定解释器进行解析):
JSONParser:解析json格式----'application/json'
FormParser:解析form提交----'application/x-www-form-urlencoded'
MultiPartParser:解析form文件上传----'multipart/form-data'
FileUploadParser解析所有格式----'*/*'