面向对象的自定义分页

1.1,整理后的数据自定义分页代码:

 1 def page_list_obj(request):
 2     li = []
 3     tatal_data_count =165#总的数据个数
 4     per_page_data = 10#每一页的数据个数
 5 
 6     per_page_count = 11#每一页显示的页码总数
 7 
 8     current_page = request.GET.get('p')  # 以get方式获取当前页面信息
 9     for i in range(tatal_data_count):
10         li.append(i+1)
11         current_page = int(current_page)#字符串转化为整数
12         start = (current_page-1)*per_page_data#数据切片开始索引
13         end = current_page*per_page_data#数据切片结束索引
14         page_data = li[start:end]#数据切片,并以列表形式传入前端

1.2,整理后的页码自定义分页代码:

 1         all_page = len(li)  # 计算出数据的个数
 2         # 求其余数,若余数为真总页面数加1,为0时页面数为求余的商
 3         count_data_qvotient, count_data_remainder = divmod(all_page, 10)  # 求商和余数
 4         if count_data_remainder:
 5             tatal_pages_count = tatal_pages_count + 1
 6         else:
 7             tatal_pages_count = count_data_qvotient
 8         # 判断总的页面数
 9         pages_all = []
10         if tatal_pages_count < per_page_count:  # 当总页面(tatal_pages_count)小于11时
11             start_index = 1
12             end_index = tatal_pages_count + 1
13         else:  # 当总页面(tatal_pages_count)分页大于11时
14             v = (per_page_count - 1) / 2
15             if current_page < v + 1:  # 当前页面需要显示的页面< v ,起始页面为1,终止页面为第tatal_pages_count + 1个页面
16                 start_index = 1
17                 end_index = tatal_pages_count + 1
18             else:  # 当前页面> v ,起始页面current_page - v,终止页面current_page + v + 1
19                 start_index = current_page - v
20                 end_index = current_page + v + 1
21                 if current_page + v > tatal_pages_count:
22                     start_index = tatal_pages_count - per_page_count
23                     end_index = tatal_pages_count + 1
24         for i in range(start_index, end_index):
25             # 如果i为当前页面加上active属性,如果不是则正常显示
26             if i == current_page:
27                 temp = ('<a class="page active"  href="/c/test4/?p=%s">%s</a>' % (i, i))
28             else:
29                 temp = ('<a class="page" href="/c/test4/?p=%s">%s</a>' % (i, i))
30             pages_all.append(temp)
31         pages_all = "".join(pages_all)  # 列表序列化

 

1.3,逻辑关系总体代码:

  1.3.1,views函数代码:

 1 """定义一个函数,当前段已get方式获取数据的时候,将数据按
 2 (tatal_data_count)  总的数据个数,
 3 (per_page_data)     每一页的数据个数参数,
 4 (per_page_count)    每一页显示的页码总数,要求为奇数,
 5 来定义每个页面显示的数据个数及页码个数。
 6 """
 7 def page_list_obj(request):
 8     li = []
 9     tatal_data_count =165#总的数据个数
10     per_page_data = 10#每一页的数据个数
11 
12     per_page_count = 11#每一页显示的页码总数
13 
14     current_page = request.GET.get('p')  # 以get方式获取当前页面信息
15     for i in range(tatal_data_count):
16         li.append(i+1)
17         current_page = int(current_page)#字符串转化为整数
18         start = (current_page-1)*per_page_data#数据切片开始索引
19         end = current_page*per_page_data#数据切片结束索引
20         page_data = li[start:end]#数据切片,并以列表形式传入前端
21 
22         all_page = len(li)  # 计算出数据的个数
23         # 求其余数,若余数为真总页面数加1,为0时页面数为求余的商
24         count_data_qvotient, count_data_remainder = divmod(all_page, 10)  # 求商和余数
25         if count_data_remainder:
26             tatal_pages_count = tatal_pages_count + 1
27         else:
28             tatal_pages_count = count_data_qvotient
29         # 判断总的页面数
30         pages_all = []
31         if tatal_pages_count < per_page_count:  # 当总页面(tatal_pages_count)小于11时
32             start_index = 1
33             end_index = tatal_pages_count + 1
34         else:  # 当总页面(tatal_pages_count)分页大于11时
35             v = (per_page_count - 1) / 2
36             if current_page < v + 1:  # 当前页面需要显示的页面< v ,起始页面为1,终止页面为第tatal_pages_count + 1个页面
37                 start_index = 1
38                 end_index = tatal_pages_count + 1
39             else:  # 当前页面> v ,起始页面current_page - v,终止页面current_page + v + 1
40                 start_index = current_page - v
41                 end_index = current_page + v + 1
42                 if current_page + v > tatal_pages_count:
43                     start_index = tatal_pages_count - per_page_count
44                     end_index = tatal_pages_count + 1
45         for i in range(start_index, end_index):
46             # 如果i为当前页面加上active属性,如果不是则正常显示
47             if i == current_page:
48                 temp = ('<a class="page active"  href="/c/test4/?p=%s">%s</a>' % (i, i))
49             else:
50                 temp = ('<a class="page" href="/c/test4/?p=%s">%s</a>' % (i, i))
51             pages_all.append(temp)
52         pages_all = "".join(pages_all)  # 列表序列化
53     return render(request,'page.html',{"li":page_data,"pages_all":pages_all})
View Code

  1.3.2,html前端代码:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <style>
 7         .pagination .page {
 8             display: inline-block;
 9             padding: 5px;
10             background-color: cyan;
11             margin: 5px;
12         }
13 
14         .pagination .page.active {
15             background-color: brown;
16             color: white;
17         }
18     </style>
19 </head>
20 <body>
21 <ul>
22     {% for item in li %}
23         <li>{{ item }}</li>
24     {% endfor %}
25 </ul>
26 
27 <div class="pagination">
28     {{ pages_all|safe }}
29 </div>
30 </body>
31 </html>
View Code

>>>转换为面向对象代码

2.1

 1 from django.shortcuts import render, HttpResponse, redirect
 2 from django.utils.safestring import mark_safe
 3 from utils import pagination
 4 
 5 
 6 # 定义一个函数,当前段已get方式获取数据的时候
 7 # 将列表li中的数据切片的形式发给前端,并显示当前页面
 8 def page_list(request):
 9     li = []
10     for i in range(165):
11         li.append(i + 1)
12         current_page = request.GET.get('p')  # 以get方式获取当前页面信息
13         current_page = int(current_page)  # 字符串转化为整数
14         obj = pagination.page(current_page, len(li), 10, 9)
15         data = li[obj.start():obj.end()]  # 切片
16         pages_all = obj.str()
17     return render(request, 'page.html', {"li": data, "pages_all": pages_all})
调用代码

2.2

封装成的类属性代码

 1 class page:
 2     def __init__(self, current_page, all_page, datas, pages):
 3         self.current_page = current_page  # current_page
 4         self.all_page = all_page
 5         self.datas = datas
 6         self.pages = pages
 7 
 8     def start(self):
 9         return (self.current_page - 1) * self.datas  # 数据切片开始索引
10 
11     def end(self):
12         return self.current_page * self.datas  # 数据切片结束索引
13 
14     def str(self):
15         w = int((self.pages - 1) / 2)  # 中间值
16         # 求其余数,若余数为真总页面数加1,为0时页面数为求余的商
17         qvotient, remainder = divmod(self.all_page, self.datas)  # 求商和余数
18         if remainder:
19             qvotient = qvotient + 1
20         pages_all = []
21         if qvotient < self.pages:  # 当总页面(count)小于11时
22             start_page = 1
23             end_page = qvotient + 1
24         else:  # 当总页面(count)分页大于11时
25             if self.current_page < w + 1:  # 当前页面需要显示的页面,<6起始页面为1,终止页面为第11个页面
26                 start_page = 1
27                 end_page = self.pages + 1
28             else:  # 当前页面大于6显示,起始页面-5,终止页面+5+1
29                 start_page = self.current_page - w
30                 end_page = self.current_page + w + 1
31                 if self.current_page + w > qvotient:
32                     print(qvotient)
33                     # 当前页面加上5之后大于总的页面数,起始页面:-11,终止页面:总的页面数加1
34                     start_page = qvotient - self.pages + 1
35                     end_page = qvotient + 1
36         for i in range(start_page, end_page):
37             # 如果i为当前页面加上active属性,如果不是则正常显示
38             # print(i)
39             if i == self.current_page:
40                 temp = ('<a class="page active"  href="/c/test4/?p=%s">%s</a>' % (i, i))
41             else:
42                 temp = ('<a class="page" href="/c/test4/?p=%s">%s</a>' % (i, i))
43             pages_all.append(temp)
44         pages_all = "".join(pages_all)  # 列表序列化
45         print(pages_all)
46         return pages_all
View Code

注意:引入py文件

调用py文件中的类

 

posted @ 2018-01-29 22:49  cerofang  阅读(205)  评论(0编辑  收藏  举报