自定义分页,复制引入即可用
1 class Pagination(object): 2 def __init__(self,totalCount,currentPage,perPageItemNum=10,maxPageNum=7): 3 # 数据总个数
4 self.total_count = totalCount 5 # 当前页
6 try: 7 v = int(currentPage) 8 if v <= 0: 9 v = 1
10 self.current_page = v 11 except Exception as e: 12 self.current_page = 1
13 # 每页显示的行数
14 self.per_page_item_num = perPageItemNum 15 # 最多显示页面
16 self.max_page_num = maxPageNum 17
18 def start(self): 19 return (self.current_page-1) * self.per_page_item_num 20
21 def end(self): 22 return self.current_page * self.per_page_item_num 23 @property 24 def num_pages(self): 25 """
26 总页数 27 :return: 28 """
29 # 666
30 # 10
31 a,b = divmod(self.total_count,self.per_page_item_num) 32 if b == 0: 33 return a 34 return a+1
35
36 def pager_num_range(self): 37 # self.num_pages()
38 # self.num_pages
39 # 当前页
40 #self.current_page
41 # 最多显示的页码数量 11
42 #self.per_pager_num
43 # 总页数
44 # self.num_pages
45 if self.num_pages < self.max_page_num: 46 return range(1,self.num_pages+1) 47 # 总页数特别多 5
48 part = int(self.max_page_num/2) 49 if self.current_page <= part: 50 return range(1,self.max_page_num+1) 51 if (self.current_page + part) > self.num_pages: 52 return range(self.num_pages-self.max_page_num+1,self.num_pages+1) 53 return range(self.current_page-part,self.current_page+part+1) 54
55 def page_str(self): 56 page_list = [] 57
58 first = "<li><a href='/fenye2.html?p=1'>首页</a></li>"
59 page_list.append(first) 60
61 if self.current_page == 1: 62 prev = "<li><a href='#'>上一页</a></li>"
63 else: 64 prev = "<li><a href='/fenye2.html?p=%s'>上一页</a></li>" %(self.current_page-1,) 65 page_list.append(prev) 66 for i in self.pager_num_range(): 67 if i == self.current_page: 68 temp = "<li class='active'><a href='/fenye2.html?p=%s'>%s</a></li>" %(i,i) 69 else: 70 temp = "<li><a href='/fenye2.html?p=%s'>%s</a></li>" % (i, i) 71 page_list.append(temp) 72
73 if self.current_page == self.num_pages: 74 nex = "<li><a href='#'>下一页</a></li>"
75 else: 76 nex = "<li><a href='/fenye2.html?p=%s'>下一页</a></li>" % (self.current_page + 1,) 77 page_list.append(nex) 78
79 last = "<li><a href='/fenye2.html?p=%s'>尾页</a></li>" %(self.num_pages,) 80 page_list.append(last) 81
82 return ''.join(page_list)
调用pager.py
views.py
1 def FenYe(request): 2 from pager import Pagination 3 current_page = request.GET.get('p') 4 page_obj = Pagination(666, current_page)#参数:totalCount,currentPage,perPageItemNum,maxPageNum
5
6 data_list = USER_LIST[page_obj.start():page_obj.end()] 7 return render(request, 'fenye.html', {'data': data_list, 'page_obj': page_obj})
fenye.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>自定制分页</title>
6
7 <link rel="stylesheet" href="/static/plugins/bootstrap/css/bootstrap.css">
8 </head>
9 <body>
10 <ul>
11 {% for row in data %} 12 <li>{{ row.name }}-{{ row.age }}</li>
13 {% endfor %} 14 </ul>
15 {% for i in page_obj.pager_num_range %} 16 <a href="/自定制分页.html?p={{ i }}">{{ i }}</a>
17 {% endfor %} 18 <hr/>
19
20 <ul class="pagination pagination-sm">
21 {{ page_obj.page_str|safe }} 22 </ul>
23 <div style="height: 300px;"></div>
24 </body>
25 </html>