1 patch与put(幂等?回顾)
PATCH 与 PUT 属性上的一个重要区别还在于:PUT 是幂等的,而 PATCH 不是幂等的。
幂等是一个数学和计算机学概念,在计算机范畴内表示一个操作执行任意次对系统的影响跟一次是相同。
2 视图组件
# 第一种方案 class List: def list(self,request): queryset = self.queryset bs = self.serializers(queryset, many=True) return JsonResponse(bs.data,safe=False) class Create: def create(self,request): print(request.data) bs = PublishSerializers(data=request.data) if bs.is_valid(): bs.save() # 生成记录 return JsonResponse(bs.data,safe=False) else: return JsonResponse(bs.errors,safe=False) class PublishView(APIView,List,Create): queryset=Publish.objects.all() serializers=PublishSerializers def get(self, request): return self.list(request) def post(self, request): # 添加一条数据 return self.create(request)
# 第二种方案 drf提供的封装的类 from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, \ DestroyModelMixin from rest_framework.generics import GenericAPIView class PublishView(GenericAPIView, ListModelMixin, CreateModelMixin): queryset = Publish.objects.all() serializer_class = PublishSerializers def get(self, request): return self.list(request) def post(self, request): # 添加一条数据 return self.create(request) class PublishDetailView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin): queryset = Publish.objects.all() serializer_class = PublishSerializers def get(self, request, pk): return self.retrieve(request, pk) def put(self, request, pk): return self.update(request, pk) def delete(self, request, pk): return self.destroy(request, pk)
# 第三种方法: from rest_framework.generics import ListCreateAPIView, ListAPIView, RetrieveUpdateDestroyAPIView class PublishView(GenericAPIView, ListModelMixin, CreateModelMixin): class PublishView(ListCreateAPIView): queryset = Publish.objects.all() serializer_class = PublishSerializers class PublishDetailView(RetrieveUpdateDestroyAPIView): queryset = Publish.objects.all() serializer_class = PublishSerializers
# 第三种: # 路由: url (r'^publish/$', views.PublishView.as_view ({'get': 'list', 'post': 'create'})), url (r'^publish/(?P\d+) ', views.PublishView.as_view ({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})), #视图 from rest_framework.viewsets import ModelViewSet class PublishView(ModelViewSet): queryset = Publish.objects.all() serializer_class = PublishSerializers # 最终: ViewSetMixin # 它有什么作用?用了它之后, 视图类中不需要写get, post, put方法了, 自己定义方法就可以了 # 让请求方法对应到自己定义的方法上, 配置路由
#路由 url(r"book/$",views.Book.as_view({'get':'test'})), url(r"book/(?P)\d+ ",views.Book.as_view({"get":"test2"})) #一个类可以写5个操作(增删改查查), from rest_framework.viewsets import ViewSetMixin # 注意先后顺序,ViewSetMixin写在前面 class TestAll(ViewSetMixin,APIView): def test(self,request): print(settings.DEBUG) return HttpResponse('test') def test2(self, request): return HttpResponse('test2') def test3(self, request): return HttpResponse('test3')
3 路由控制
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^books/$', views.BookView.as_view()), url(r'^books/(?P\d+)$ ', views.BookDetailView.as_view()), ]
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^publish/$', views.PublishView.as_view({'get':'list','post':'create'})), url(r'^publish/(?P\d+)/$ ', views.PublishView.as_view({'get':'retrieve','put':'update','delete':'destroy'})), ] from rest_framework.viewsets import ModelViewSet class PublishView(ModelViewSet): queryset=models.Publish.objects.all() serializer_class=PublishSerializers
from django.conf.urls import url,include from app01 import views from rest_framework import routers router=routers.DefaultRouter() # 两个参数,一个是匹配的路由,一个是视图中写的CBV的类 router.register('publish',views.PublishView) urlpatterns = [ # http://127.0.0.1:8000/publish/format=json(渲染器通过这个判断,返回渲染的页面) # url(r'^publish/', views.PublishView.as_view({'get':'list','post':'create'})), # http://127.0.0.1:8000/publish.json(渲染器通过这个判断,返回渲染的页面) # url(r'^publish\.(?P\w+)$', views.PublishView.as_view({'get':'list','post':'create'})), # 可以用 以下方式访问 # 1 http://127.0.0.1:8000/publish/ # 2 http://127.0.0.1:8000/publish.json # 3 http://127.0.0.1:8000/publish/3 # 4 http://127.0.0.1:8000/publish/3.json url(r'',include(router.urls)) ] from rest_framework.viewsets import ModelViewSet class PublishView(ModelViewSet): queryset=models.Publish.objects.all() serializer_class=PublishSerializers
4 响应器
作用:
根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件。
用户请求URL:
http://127.0.0.1:8000/test/?format=json
http://127.0.0.1:8000/test.json
内置响应器:
#内置响应器 #显示json格式:JSONRenderer #默认显示格式:BrowsableAPIRenderer(可以修改它的html文件) #表格方式:AdminRenderer #form表单方式:HTMLFormRenderer #使用(基本上不需要你配置): #局部配置 #视图类中: from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer renderer_classes=[JSONRenderer,BrowsableAPIRenderer] #全局配置 #在setting中: REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES':['rest_framework.renderers.JSONRenderer'] }
视图组件总结:
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin,DestroyModelMixin
#类内只有一个方法分别实现了list:get,create:post,|单条操作 retrieve:get,update:put,destroy:delete
from rest_framework.generics import GenericAPIView, ListAPIView, CreateAPIView, ListCreateAPIView, RetrieveUpdateDestroyAPIView
#GenericAPIView:提供queryset,serializers与ListModelMixin, CreateModelMixin等一起用(解耦和,独特)
#ListAPIView:继承了ListModelMixin 内部定义了get方法 () #后面的一样
from rest_framework.viewsets import ModelViewSet
#ModelViewSet:get,post,put方法了,自己定义方法就可以了让请求方法对应到自己定义的方法上,配置路由
from rest_framework.response import Response
#返回对象可以是Response注册了rest_framework有界面/json格式选择,Jsonresponse只有json格式,