依据上文,需要借助http工具
添加format_suffix_patterns来解决
导入from rest_framework. urlpatterns import format_suffix_patterns
urlpatterns= format_suffix_patterns ( urlpatterns)
from rest_framework. views import APIView
from rest_framework. response import Response
from rest_framework import status
from app001 import serializers
from app001 import models
from django. http import Http404
from rest_framework import mixins
from rest_framework import generics
使用apiview手动去写请求
# class PublisherList ( APIView) :
# "" "列出所有的出版社,或者创建一个新的出版社" ""
# def get ( self, request, format= None) :
# querset= models. Pbulisher. objects. all ( ) #查询出所有的出版社
# s= serializers. PublisherSerializer ( querset, many= True)
# return Response ( s. data)
#
# def post ( self, request, format= None) :
# s= serializers. PublisherSerializer ( data= request. data)
# if s. is_valid ( ) : #如果数据没有问题
# s. save ( )
# return Response ( s. data, status= status. HTTP_201_CREATED )
# class PublisherDetail ( APIView) :
# "" "具体的出版社,查看、修改、删除视图" ""
# def get_object ( self, pk) :
# try :
# return models. Pbulisher. objects. get ( pk= pk)
# except models. Pbulisher. DoesNotExist:
# return Http404
#
# def get ( self, request, pk, format= None) :
# publisher= self. get_object ( pk)
# s= serializers. PublisherSerializer ( publisher)
# return Response ( s. data)
#
# def put ( self, request, pk, format= None) :
# publisher= self. get_object ( pk)
# s= serializers. PublisherSerializer ( publisher, data= request. data)
# if s. is_valid ( ) :
# s. save ( )
# return Response ( s. data)
# return Response ( s. errors, status= status. HTTP_400_BAD_REQUEST )
#
# def delete ( self, request, pk, format= None) :
# publisher= self. get_object ( pk)
# publisher. delete ( )
# return Response ( status= status. HTTP_204_NO_CONTENT )
使用混合类简化上面的书写
class PublisherList ( mixins. ListModelMixin,
mixins. CreateModelMixin,
generics. GenericAPIView) :
queryset= models. Pbulisher. objects. all ( )
serializer_class= serializers. PublisherSerializer
def get ( self, request, * args, ** kwargs) :
return self. list ( request, * args, ** kwargs) #这个list就是ListModelMixin里面的list
def post ( self, request, * args, ** kwargs) :
return self. create ( request, * args, ** kwargs) #这个craete就是CreateModelMixin里面create
class PublisherDetail ( mixins. RetrieveModelMixin, #获取某一个具体的对象
mixins. UpdateModelMixin,
mixins. DestroyModelMixin,
generics. GenericAPIView) :
queryset = models. Pbulisher. objects. all ( )
serializer_class= serializers. PublisherSerializer
def get ( self, request, * args, ** kwargs) :
return self. retrieve ( request, * args, ** kwargs)
def put ( self, request, * args, ** kwargs) :
return self. update ( request, * args, ** kwargs)
def delete ( self, request, * args, ** kwargs) :
return self. delete ( request, * args, ** kwargs)
用别人混合好的类再次进行进行简化
class PublisherList ( generics. ListCreateAPIView) :
queryset = models. Pbulisher. objects. all ( )
serializer_class = serializers. PublisherSerializer
class PublisherDetail ( generics. RetrieveUpdateDestroyAPIView) :
queryset = models. Pbulisher. objects. all ( )
serializer_class = serializers. PublisherSerializer
from django. urls import path, re_path
from . import views
urlpatterns= [
path ( "publishers/" , views. PublisherList. as_view ( ) , name= 'publisher_list' ) ,
re_path ( "publishers/(?P<pk>[0-9]+)$" , views. PublisherDetail. as_view ( ) , name= 'publisher_detail' ) ,
]