Django自定义分页类的封装

 1.自定义分页类的封装:

 1 class Pagination(object):
 2     """
 3     基于bootstrap样式的自定义分页类的封装
 4     """
 5     # page_num为每页显示的数量,max_page_num为每页显示的总页码数
 6     def __init__(self, request, lengths, page_num=10, max_page_num=7):
 7 
 8         # 首先进行异常错误处理,比如:.../?page=-5等
 9         try:
10             # 获取url中的page值,并将默认值设置为1
11             page = int(request.GET.get(page, 1))
12             # print(page, type(page))
13             if page < 1:
14                 page = 1
15         except Exception:
16             page = 1
17 
18         # 每页显示的数量
19         # page_num = 7
20 
21         # 计算总页码数
22         # divmod为len(user_list) / page_num,整数为total_num,余数为remainder
23         total_num, remainder = divmod(lengths, page_num)
24         if remainder != 0:
25             total_num += 1
26 
27         # 每页显示的总页码数
28         # max_page_num = 7
29 
30         # 每页显示总页码数一半数
31         half_num = max_page_num // 2
32         # 实际总页码数 < 页面总页码数
33         if total_num < max_page_num:
34             # 页码起始值
35             page_start = 1
36             # 页码终止值
37             page_end = total_num
38         # 实际总页码数 > 页面总页码数
39         else:
40             # 处理左边极值
41             if page - half_num < 1:
42                 page_start = 1
43                 page_end = max_page_num
44             # 处理右边极值
45             elif page + half_num > total_num:
46                 page_start = total_num - max_page_num + 1
47                 page_end = total_num
48             else:
49                 page_start = page - half_num
50                 page_end = page + half_num
51 
52         html_list = [<nav aria-label="Page navigation"><ul class="pagination pagination-lg">]
53 
54         if page == 1:
55             html_list.append(
56                 <li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>)
57         else:
58             html_list.append(
59                 <li><a href="?page=%s" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li> % (page - 1))
60 
61         for i in range(page_start, page_end + 1):
62             if page == i:
63                 html_list.append(<li class="active"><a href="?page=%s">%s</a></li> % (i, i))
64             else:
65                 html_list.append(<li><a href="?page=%s">%s</a></li> % (i, i))
66 
67         if page == total_num:
68             html_list.append(
69                 <li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>)
70         else:
71             html_list.append(
72                 <li><a href="?page=%s" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li> % (
73                         page + 1))
74 
75         html_list.append(</ul></nav>)
76 
77         self.html_list = ‘‘.join(html_list)
78 
79         # 起始
80         # start = 0
81         self.start = (page - 1) * page_num
82 
83         # 终止
84         # end = 7
85         self.end = page * page_num

2.后端调用

1  def GoodType(request): 
2     resp_data = {}
3     g_type = GoodsType.objects.all()
4     page = Pagination(request,lengths=g_type.count(),page_num=5)
5     resp_data[g_type] =g_type[page.start:page.end]
6     resp_data[page_html] = page.html_list
7     return render(request, goods/goods_type.html, resp_data)

3.前端使用

1   <div class="row" style="margin-right: 0">{{ page_html|safe }}</div>

效果展示:

Django自定义分页类的封装

 

参考博客:戳这里

 

Django自定义分页类的封装

上一篇:【K8s教程】K8s包管理工具helm介绍


下一篇:funny flow