首页 > 其他分享 >分页器

分页器

时间:2022-12-12 17:46:02浏览次数:45  
标签:分页 views py page 条数 class size

目录

三种分页方式:

  • PageNumberPagination
  • LimitOffsetPagination
  • CursorPagination
#settings.py全局配置
REST_FRAMEWORK = {
    'PAGE_SIZE': 2,
}

PageNumberPagination

# urls.py
path('books2/', views.BookListView.as_view()),#继承了ListAPIView


#views.py
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination

#重写
class MyPageNumberPagination(PageNumberPagination):
    page_size = 3  # 每页显示的条数
    # page_query_param = 'num'  # 前端发送的页数关键字,默认为'page'
    max_page_size = 5  # 每页最大显示条数
    page_size_query_param = 'size'#每一页显示的条数


class BookListView(ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookModelSerializer
    # 配置分页
    pagination_class = MyPageNumberPagination
    
    
#http://127.0.0.1:8000/api/books2/?num=2
#http://127.0.0.1:8000/api/books2/?num=2&size=6   #一页最多显示5数据

image-20221210192345931

image-20221210192630387

LimitOffsetPagination

# urls.py
path('books2/', views.BookListView.as_view()),#继承了ListAPIView


#views.py
class MyLimitOffsetPagination(LimitOffsetPagination):
    default_limit = 3     #每页条数
    limit_query_param = 'limit'   #往后拿几条
    offset_query_param = 'offset'   #从第几个开始
    max_limit = 5

class BookListView(ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookModelSerializer
    # 配置分页

    pagination_class = MyLimitOffsetPagination
    
#http://127.0.0.1:8000/api/books2/?offset=6&limit=2       

image-20221210192918656

CursorPagination

写出来只有上一页,下一页,不能跳

# urls.py
path('books2/', views.BookListView.as_view()),#继承了ListAPIView


#views.py
class MyCursorPagination(CursorPagination):
    page_size = 2  # 每一页显示的条数
    cursor_query_param = 'cursor'  # 每一页查询的key
    ordering = '-id'  # 排序


class BookListView(ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookModelSerializer
    # 配置分页

    pagination_class = MyCursorPagination

image-20221210193535346

image-20221210193740135

使用APIView写分页

#urls.py
path('books3/', views.BookView.as_view()),

#views.py
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
from rest_framework.views import APIView
from rest_framework.response import Response
from app01.models import Book
from app01.serialize import BookModelSerializer

#重写
class MyPageNumberPagination(PageNumberPagination):
    page_size = 3  # 每页显示的条数
    page_query_param = 'num'  # 前端发送的页数关键字,默认为'page'
    max_page_size = 5  # 每页最大显示条数
    page_size_query_param = 'size'  # 每一页显示的条数
    
class BookView(APIView):
    def get(self, request, *args, **kwargs):
        book_list = Book.objects.all()
        # 实例化得到分页器对象
        page_cursor = MyPageNumberPagination()
        book_list = page_cursor.paginate_queryset(book_list, request, view=self)
        next_url = page_cursor.get_next_link()  # 下一页
        pre_url = page_cursor.get_previous_link()  # 上一页
        print(next_url)#打印下一页的url
        print(pre_url)#打印下一页的url
        book_ser = BookModelSerializer(book_list, many=True)
        return Response(data=book_ser.data)
        

image-20221210195511467

image-20221210195526353

标签:分页,views,py,page,条数,class,size
From: https://www.cnblogs.com/zaosong/p/16976725.html

相关文章

  • 利用延迟关联或者子查询优化超多分页场景
      EXPLAINSELECT*FROM`demo`limit300000,20   EXPLAINSELECTa.*FROMdemoa,(selectidfromdemoLIMIT300000,20)bwherea.id=b.id......
  • 自定义分页器
    自定义分页器目录自定义分页器1、分页推导2、分页器代码封装1、分页推导queryset对象支持切片操作确定用户访问的页码url?page=1current_page=request.GET.......
  • 解决mybatis一对多嵌套查询,解决分页数据少了的问题
    转载自:https://blog.csdn.net/zjun1001/article/details/117671517 问题在用mybatis做一对多查询时候,常用collection配合完成结果查询。在不涉及分页查询情况下,查询结果......
  • 分页器
    分页器目录分页器PageNumberPaginationLimitOffsetPaginationCursorPagination使用APIView写分页三种分页方式:PageNumberPaginationLimitOffsetPaginationCursorPag......
  • 从零实现一个操作系统内核——寻址机制:段机制与分页机制
    1.寻址的通俗理解对于一个处理器来说,可以访问的地址空间大小取决于它自身地址线的数量(有些不严谨哈),这个地址空间是cpu可以访问的最大内存空间,实际上可能小于计算机实际拥......
  • oracle数据库实现分页查询
    SELECT*FROM ( SELECT T.*, ROWNUMRN FROM (SELECT*FROM表名WHERE条件)T WHERE ROWNUM<=((currentPage-1)*pageSize+pageSize) )......
  • 分页器
    1.自己封装好的源码classPagination(object):  def__init__(self,current_page,all_count,per_page_num=2,pager_count=11):    """    封装分......
  • 自定义分页样式
    一.基本安装和配置1.pipinstalldjango-pure-pagination2.settings.py中installapp中添加'pure_pagination',3.settings.py中添加参数PAGINATION_SETTINGS={......
  • Java操作ElasticSearch(四、排序、高亮、分页、Filter过滤、source筛选)
    排序通过SearchSourceBuilder的sort(String,SortOrder)方法用来实现排序条件的封装@Testpublicvoidtest18()throwsIOException{SearchRequestrequest......
  • 分页查询优化
    看原始的SQL语句SELECT*FROM`discuss_post`limit300000,5另外可以看到这里并没有走索引比如这里会查询前300005条数据,然后丢掉前面的300000条数据对于ID自增......