• 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})
    

      

  • 相关阅读:
    C语言-第四周作业
    第8次Scrum会议(10/20)【欢迎来怼】
    例行报告(20171011-20171019)
    C语言--第二周作业评分和总结(5班)
    C语言-第三周作业
    第一次Scrum会议(10/13)【欢迎来怼】
    单元测试之四则运算
    四则运算V1.1
    例行报告(20170927-20171010)
    C语言--第一周作业评分和总结(5班)
  • 原文地址:https://www.cnblogs.com/Xingtxx/p/10729190.html
Copyright © 2020-2023  润新知