url定制和渲染器:
渲染器:
如果不对渲染器进行设置,默认使用常用url设置方式,浏览器端需要展示不同的渲染方式的数据时,需要在url地址上加上参数format,例如:
path(‘author/‘, views.AuthorView.as_view()),
class AuthorView(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView): queryset = models.Author.objects.all() serializer_class = serializers.AuthorSerializers def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs) def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs)
http://127.0.0.1:8000/author/?format=json, 即显示json数据
http://127.0.0.1:8000/author/?format=api, 即显示浏览器格式API
url路径定制:
定制后就能以url路径的形式访问不同的渲染器渲染后的数据:url路径参数名必须是format
re_path(‘^book\.(?P<format>[a-z0-9]+)$‘, views.BookUrlView.as_view())
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer class BookUrlView(APIView): # renderer_classes = [JSONRenderer, BrowsableAPIRenderer] def get(self, request, *args, **kwargs): book_list = models.Book.objects.all() print(book_list) ser = serializers.BookSerializers(book_list, many=True, context={‘request‘: request}) return Response(ser.data)
http://127.0.0.1:8000/book.json
http://127.0.0.1:8000/book.api
他们的效果与上面是相同的,只是一种是基于url传参,一种是基于url路径
rest_framework默认使用两种渲染器:json和浏览器api
还有其他几种渲染器:
如果没定制url,配置这些渲染器后,就能这样访问,
表格:http://127.0.0.1:8000/book?format=admin
form表单:http://127.0.0.1:8000/book?format=form
自定义显示模板:http://127.0.0.1:8000/book?format=html
如果用定制了url路径:
re_path(‘^book\.(?P<format>[a-z0-9]+)$‘, views.BookUrlView.as_view())
表格:http://127.0.0.1:8000/book.admin
form表单:http://127.0.0.1:8000/book.form
自定义显示模板:http://127.0.0.1:8000/book.html
总结:
渲染器设置:
1、默认使用json和浏览器api,可以在settings中设置为全局,局部设置类参数renderer_classes
class BookUrlView(APIView): # renderer_classes = [JSONRenderer, BrowsableAPIRenderer] def get(self, request, *args, **kwargs): book_list = models.Book.objects.all() print(book_list) ser = serializers.BookSerializers(book_list, many=True, context={‘request‘: request}) return Response(ser.data)
2、如果同时多个存在渲染器时,自动根据URL后缀来选择渲染器。只有一个时,不用加后缀也能访问
url路径定制:
1、参数名必须是format
re_path(‘^book\.(?P<format>[a-z0-9]+)$‘, views.BookUrlView.as_view())
2、可以手动,也可以全自动生成这些url,导入routers,创建DefaultRouter对象,将路径名与视图注册。
from django.conf.urls import url, include from rest_framework import routers from web.views import s10_generic router = routers.DefaultRouter() router.register(r‘users‘, s10_generic.UserViewSet) urlpatterns = [ url(r‘^‘, include(router.urls)), # url(r‘^v1/‘, include(router.urls)), # 区分版本 ]
分页:
rest_framework提供三种分页方式
1、根据页码分页:PageNumberPagination
page_size默认为None,可以自己在settings中配置,也可以继承这个类自定义一些属性
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination class MyPageNumberPagination(PageNumberPagination): page_size = 2 # 默认每页显示多少条 page_query_param = ‘page‘ # 页码别名 page_size_query_param = "max" # 最多显示多少条 http://127.0.0.1:8000/book.api?page=1&max=5 max_page_size = 5 # 最多显示多少条, 配合page_size_query_param使用 class BookUrlView(APIView): # renderer_classes = [JSONRenderer, BrowsableAPIRenderer] # 渲染器 def get(self, request, *args, **kwargs): book_list = models.Book.objects.all() page = MyPageNumberPagination() # 创建分页对象 page_book = page.paginate_queryset(book_list, request, view=self) # 分页处理 ser = serializers.BookSerializers(page_book, many=True, context={‘request‘: request}) return Response(ser.data)
2、根据位置和个数进行分页:LimitOffsetPagination
class MyLimitOffsetPagination(LimitOffsetPagination): default_limit = 2 # 默认每页显示多少条 limit_query_param = ‘limit‘ # URL中传入的显示数据条数的参数 http://127.0.0.1:8000/book.api?page=2&offset=5&limit=2 offset_query_param = ‘offset‘ # URL中传入的数据位置的参数 max_limit = None # 最大每页显得条数 class BookUrlView(APIView): # renderer_classes = [JSONRenderer, BrowsableAPIRenderer] # 渲染器 def get(self, request, *args, **kwargs): book_list = models.Book.objects.all() page = MyLimitOffsetPagination() # 创建分页对象 page_book = page.paginate_queryset(book_list, request, view=self) # 分页处理 ser = serializers.BookSerializers(page_book, many=True, context={‘request‘: request}) return Response(ser.data)
3、游标分页:CursorPagination,页码进行了加密,需要自动生成url地址才能对上一页下一页方法
class MyCursorPagination(CursorPagination): cursor_query_param = ‘cursor‘ # URL传入的游标参数 page_size = 2 # 默认每页显示的数据条数 page_size_query_param = "max" # URL传入的每页显示条数的参数 max_page_size = 2 # 每页显示数据最大条数 ordering = ‘id‘ # 根据ID从小到大排列 # 根据ID从大到小排列 # ordering = ‘-created‘ class BookUrlView(APIView): # renderer_classes = [JSONRenderer, BrowsableAPIRenderer] # 渲染器 def get(self, request, *args, **kwargs): book_list = models.Book.objects.all() page = MyPageNumberPagination() # 创建分页对象 page_book = page.paginate_queryset(book_list, request, view=self) # 分页处理 ser = serializers.BookSerializers(page_book, many=True, context={‘request‘: request}) return page.get_paginated_response(ser.data) # CursorPagination类的页码进行了加密,需要用这种显示上一页、下一页
get_paginated_response会自动生成上一页和下一页的url地址,使用CursorPagination分页类必须用这种方法返回,否则自己无法生写访问上一页下一页的数据,它实际上是在response响应器上进行了封装,所以只需要将它返回即可,不需要再用响应器返回。
原文地址:https://www.cnblogs.com/aizhinong/p/12569172.html