Django Rest Framework --- 分页器组件

一、简单分页

简单分页就是查看第n页,每条n数据

  • 使用配置文件

在settings.py中配置

REST_FRAMEWORK = {
    # 每页显示两条
    'PAGE_SIZE':2
}
  • views.py
from rest_framework.pagination import PageNumberPagination

class BooksView(APIView):
    def get(self,request):
        book_list = models.Book.objects.all()
        page_pagination = PageNumberPagination()
        page_list = page_pagination.paginate_queryset(book_list,request,view=self)
        ser = BookSerializer(instance=page_list,many=True)
        print(ser.data)
        return Response(ser.data)
  • 自定义四个参数
from rest_framework.pagination import PageNumberPagination

class BooksView(APIView):
    def get(self,request):
        #使用方法
        #1.导入PageNumberPagination类
        #2.实例化产生一个对象
            #-page_pagination = PageNumberPagination()
        #3.需要配置四个参数: page_size, page_query_param, page_size_query_param, max_page_size
            #-page_size必须指定: 在setting中指定或者直接赋值
        #4.调用下面的方法, 返回ret数据
            #-ret = page_pagination.paginate_queryset(book_list, request, self)
        # 5.再序列化,就是序列化返回的数据,也就是ret
        book_list = models.Book.objects.all()
        page_pagination = PageNumberPagination()
        #四个参数
        page_pagination.page_size = 4   #每页的条数
        page_pagination.page_query_param = 'ccc'  #控制查询第几页的查询参数,
        page_pagination.page_size_query_param = 'max'  #用户控制每页显示的条数
        page_pagination.max_page_size = 6  #每页显示的最大条数,防止用户自定义的条数太大
        page_list = page_pagination.paginate_queryset(book_list,request,view=self)
        ser = BookSerializer(instance=page_list,many=True)
        print(ser.data)
        return Response(ser.data)

二、偏移分页

from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination

class BooksView(APIView):
    def get(self,request):
        #使用方法:同简单分页
        book_list = models.Book.objects.all()
        page_pagination = LimitOffsetPagination()
        page_pagination.default_limit = 5  #默认显示的每页数量,默认偏移的数量
        page_pagination.limit_query_param = 'xx'    #往后偏移多少条,默认是limit,也可以自定义
        page_pagination.offset_query_param = 'ee'   #标杆值,默认是offser,也可以自定义
        # limit_query_param + offset_query_param联合起来用:
        # -访问: http://127.0.0.1:8000/books/?limit=1&offset=5   表示: 以数据的第5条作为标杆, 往后偏移1条
        page_list = page_pagination.paginate_queryset(book_list,request,view=self)
        ser = BookSerializer(instance=page_list,many=True)
        print(ser.data)
        return Response(ser.data)

三、CursorPagination(加密分页,只能看上一页和下一页,速度快)

from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination

class BooksView(APIView):
    def get(self,request):
        #使用方法:同简单分页
        #重点参数:
        #page_size: 每页显示的条数
        #cursor_query_param: 不需要动
        #ordering: 按什么排序
        #通过get_paginated_response返回结果中带上一页和下一页的链接地址
        #page_pagination.get_paginated_response(book_ser.data)
        #方法的用法
        book_list = models.Book.objects.all()
        page_pagination = CursorPagination()
        page_pagination.ordering = 'id'
        page_list = page_pagination.paginate_queryset(book_list,request,view=self)
        ser = BookSerializer(instance=page_list,many=True)
        print(ser.data)
        return page_pagination.get_paginated_response(ser.data)

 

上一篇:luogu_2107【题解】


下一篇:Restful framework【第九篇】分页器