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