zoukankan      html  css  js  c++  java
  • Django 分页处理

    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    
    class ShowCousesStop(APIView):
    
        permission_classes = [IsAdminUser]
    
        def get(self, request):
            contact_list = Stop.objects.all()
            paginator = Paginator(contact_list, 25)  # Show 25 contacts per page
    
            page = request.GET.get('page')
            try:
                contacts = paginator.page(page)
            except PageNotAnInteger:
                # If page is not an integer, deliver first page.
                contacts = paginator.page(1)
            except EmptyPage:
                # If page is out of range (e.g. 9999), deliver last page of results.
                contacts = paginator.page(paginator.num_pages)
    
            print(contacts.paginator.num_pages, "num_pages 总页数")
            print(contacts.has_previous(),"has_previous 是否有上一页")
            print(contacts.has_other_pages(), "has_other_pages  其他页")
            print(contacts.next_page_number(),"next_page_number,下一页的页码值")
            try:
                print(contacts.previous_page_number(),"previous_page_number,上一页的页码值")
            except EmptyPage as e:
                print("页码少于1",e)
            print(contacts.paginator.page_range,"page_range,总页数范围")
            print(contacts.has_next(),"has_next:是否有下一页")
            print(contacts.number,"当前页码值")
    
            """
            3 num_pages 总页数
            False has_previous 是否有上一页
            True has_other_pages  其他页
            2 next_page_number,下一页的页码值
            页码少于1 页号少于1
            range(1, 4) page_range,总页数范围
            True has_next:是否有下一页
            1 当前页码值
            """
            # for i in contacts:
    
                # print(i.user_id)
    
            # print(contacts, type(contacts))
            # data=AdminCourseStopSerializer(contacts)
            date=""
            return Response(data={"contacts": date})
    

      

  • 相关阅读:
    虚幻4游戏开发_3_创建与继承材质
    Python 之 读取txt文件
    Guava ---- Concurrent并发
    leetCode 67.Add Binary (二进制加法) 解题思路和方法
    poj 1331 Multiply
    二叉树的三叉存储
    FTPClientUtil FTPclient工具
    HDU1018 Big Number n!的位数
    MyBatis參数格式化异常解决方式:MyBatisSystemException:
    Spark:大数据的电花火石!
  • 原文地址:https://www.cnblogs.com/Xingtxx/p/10729190.html
Copyright © 2011-2022 走看看