Django 分页处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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})

  

posted @   Xingtxx  阅读(393)  评论(6编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示