• 分页器


    一、常规分页:查看第n页,每页显示n条

    url.py

    url(r'^publish/', views.Publish.as_view())

    views.py

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.pagination import PageNumberPagination
    from app01 import models
    from rest_framework.serializers import ModelSerializer
    
    
    class PublishSerializers(ModelSerializer):
        class Meta:
            model = models.Publish
            fields = '__all__'
    
    
    class Publish(APIView):
        def get(self, request, *args, **kwargs):
            # 批量添加数据
            #  ll = []
            #  for i in range(100):
            #      ll.append(models.Publish(name='%s出版社' % i, city='%s城市' % i))
            #
            #  models.Publish.objects.bulk_create(ll)
            #  return Response()
    
            # 查询出所有数据
            ret = models.Publish.objects.all()
            # 实例化产生一个普通分页对象
            page = PageNumberPagination()
            # 每页显示多少条,也可以在settings里设置
            page.page_size = 4
            # 查询指定查询哪一页的key值
            page.page_query_param = 'page'
    
            # 前端控制每页显示多少条的查询key值比如size=9,表示一页显示9条
            page.page_size_query_param = 'size'
            # 控制每页最大显示多少,size如果传100,最多也是显示10
            page.max_page_size = 10
            ret_page = page.paginate_queryset(ret, request, self)
            # 序列化
            pub_ser = PublishSerializers(ret_page, many=True)
            # 去setting中配置每页显示多少条
    
            return Response(pub_ser.data)

    settings.py

    REST_FRAMEWORK = {
        'PAGE_SIZE': 3  # 表示每页显示三条
    }

    前端展示:

    二、偏移分页:在第n个位置,向后查看n条数据

    urls.py

    url(r'^publish/', views.Publish.as_view())

     views.py

    from rest_framework.views import APIView
    from app01 import models
    from rest_framework.pagination import LimitOffsetPagination
    from rest_framework.serializers import ModelSerializer
    
    
    class PublishSerializers(ModelSerializer):
        class Meta:
            model = models.Publish
            fields = '__all__'
    
    
    class Publish(APIView):
         def get(self, request, *args, **kwargs):
            # 批量添加
            #  ll = []
            #  for i in range(100):
            #      ll.append(models.Publish(name='%s出版社' % i, city='%s城市' % i))
            #
            #  models.Publish.objects.bulk_create(ll)
            #  return Response()
       
    
            ret = models.Publish.objects.all()
            # 实例化产生一个偏移分页对象
            page = LimitOffsetPagination()
            # 四个参数:
            # 从标杆位置往后取几个,默认取3个,我可以指定,# 可以去setting中配置每页显示多少条
            page.default_limit = 3
            # 每次取得条数
            page.limit_query_param = 'limit'
            # 标杆值,现在偏移到哪个位置,如果offset=6 表示当前在第6条位置上,往后取
            page.offset_query_param = 'offset'
            # 最大取10条
            page.max_limit = 10
            ret_page = page.paginate_queryset(ret, request, self)
            # 序列化
            pub_ser = PublishSerializers(ret_page, many=True)
            # 调用这个方法返回的数据中会有总条数,上一页地址,下一页地址
            return page.get_paginated_response(pub_ser.data)

    settings.py

    REST_FRAMEWORK = {
        'PAGE_SIZE': 3  # 表示每页显示三条
    }

    前端展示

    三、cursor游标方式:加密分页,只能看上一页和下一页,速度快

    urls.py

    url(r'^publish/', views.Publish.as_view())

     views.py

    from rest_framework.views import APIView
    from app01 import models
    from rest_framework.pagination import CursorPagination
    from rest_framework.serializers import ModelSerializer
    
    
    class PublishSerializers(ModelSerializer):
        class Meta:
            model = models.Publish
            fields = '__all__'
    
    
    class Publish(APIView):
        def get(self, request, *args, **kwargs):
            # 批量添加
            #  ll = []
            #  for i in range(100):
            #      ll.append(models.Publish(name='%s出版社' % i, city='%s城市' % i))
            #
            #  models.Publish.objects.bulk_create(ll)
            #  return Response()
    
            ret = models.Publish.objects.all()
            # 实例化产生一个偏移分页对象
            page = CursorPagination()
            # 三个参数:
            # 每页显示多少条,也可以在settings里设置
            page.page_size = 3
            # 查询的key值
            page.cursor_query_param = 'cursor'
            # 按什么排序
            page.ordering = 'id'
            ret_page = page.paginate_queryset(ret, request, self)
            # 序列化
            pub_ser = PublishSerializers(ret_page, many=True)
            # 去setting中配置每页显示多少条
            # 调用这个方法返回的数据中会有总条数,上一页地址,下一页地址
            return page.get_paginated_response(pub_ser.data)

    settings.py

    REST_FRAMEWORK = {
        'PAGE_SIZE': 3  # 表示每页显示三条
    }

    前端展示

  • 相关阅读:
    有效的数独
    两个数组的交集
    单值二叉树
    go实现二叉树
    合并两个有序链表
    重塑矩阵
    CompantScan 今天碰到问题记录
    报错:expected at least 1 bean which qualifies as autowire candidate(Spring Bean 无法注入)
    The attempt was made from the following location: com.ruoyi.framework.config.ResourcesConfig.corsFilter(ResourcesConfig.java:57)
    2022年官网下安装ZooKeeper最全版与官网查阅方法
  • 原文地址:https://www.cnblogs.com/zhangguosheng1121/p/11140893.html
Copyright © 2020-2023  润新知