Django 分页处理

∥☆過路亽.° 提交于 2020-05-05 12:03:44
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})

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!