一、views.py操作
1 from app01.pager import Pagination
2 def index2(request):
3 curremt_page = request.GET.get('p')
# 获取页面分页信息
4 obj_list = Pagination(666,curremt_page) # 传入的current_page数据是经过url过滤的,只会是数字,为整数,所以定义类中只需要考虑用户输入过大
# 获取数据信息
5 data_list = USER_LIST[obj_list.start():obj_list.end()]
6 return render(request,'index2.html',{'data':data_list,'obj_list':obj_list})
1 #接收post传递数据
2 def post(self, page):
3 user = self.get_argument('username')
4 email = self.get_argument('email')
5 temp = {'username':user,'email':email}
6 LIST_INFO.append(temp)
7 self.redirect('/index/'+page)
CBVf方式提交表单时
二、自定义一个模块pager.py,并创建类
1 class Pagination(object):
2 def __init__(self,totalCount,currentPage,perPageItemNum=15,maxPageNum=7):
3 # 所有数据总个数
4 self.total_count = int(totalCount)
5 # 当前页
6 try:
7 v = int(currentPage)
8 if v <= 0:
9 v = 1
10 self.current_page = v
11
12 except Exception as e:
13 self.current_page = 1
14 # 每页显示行数
15 self.per_page_item_num = int(perPageItemNum)
16 # 最多页面数
17 self.max_page_num = int(maxPageNum)
18
19 def start(self): # 获取当前页结束条数
20 return (self.current_page - 1)*self.per_page_item_num
21
22 def end(self): # 获取当前页开始条数
23 return self.current_page*self.per_page_item_num
24
25 @property
26 def num_pages(self): # 获取总页数
27 a,b = divmod(self.total_count,self.per_page_item_num)
28 if b == 0:
29 return a
30 return a+1
31
32 @property
33 def pager_num_range(self): # 获取分页栏的页码范围,返回列表
34 # 当前页
35 # self.current_page
36 # 最多显示页码数 7
37 # self.per_page_num
38 # 总页数
39 # self.num_pages
40 # 判断最小极值
41 if self.max_page_num > self.num_pages:
42 # 根据当前页动态生成,设置页面显示数
43 return range(1, self.num_pages + 1)
44 # 总页数特别多时
45 part = int(self.max_page_num/2)
46
47 if part >= self.current_page:
48 return range(1, self.max_page_num + 1)
49
50 # 判断最大极值
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
54 return range(self.current_page - part, self.current_page + part +1)
55
56 def page_str(self): # 返回模板页面需要显示的HTML,需要进行还原{% safe|xxx %}
57 page_list = []
58
59 if self.current_page ==1:
60 prev = "<li><a href='#'>上一页</a></li>"
61 else:
62 prev = "<li><a href='/index2.html?p=%s'>上一页</a></li>" %(self.current_page-1)
63 page_list.append(prev)
64
65 first = "<li><a href='/index2.html?p=1'>首页</a></li>"
66 page_list.append(first)
67
68 for i in self.pager_num_range:
69 if self.current_page == i:
70 tem = "<li class='active'><a href='/index2.html?p=%s'>%s</a></li>" % (i,i)
71 else:
72 tem = "<li><a href='/index2.html?p=%s'>%s</a></li>" % (i, i)
73 page_list.append(tem)
74
75 last = "<li><a href='/index2.html?p=%s'>尾页</a></li>" % self.num_pages
76 page_list.append(last)
77
78 if self.current_page == self.num_pages:
79 nex = "<li><a href='#'>下一页</a>"
80 else:
81 nex = "<li><a href='/index2.html?p=%s'>下一页</a></li>" % (self.current_page + 1)
82 page_list.append(nex)
83
84 return ''.join(page_list)
三、HTML操作
先引入bootstrap
1 <head>
2 <meta charset="UTF-8">
3 <title>Title</title>
4 <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css" />
5 </head>
6 <body>
7
8 <ul>
9 {% for row in data %}
10 <li>{{row.name}}-{{row.age}}</li>
11 {% endfor %}
12 </ul>
13
14 <nav aria-label="Page navigation">
15 <ul class="pagination">
16 {{obj_list.page_str|safe}}
17 </ul>
18 </nav>
19
20 </body>
四、总结
1 - 自定义分页
2 传入:
3 - 所有数据的个数
4 - 每页行数
5 - 当前页
6 - 最多页面数