Django分页组件

1.设计思路

根据每页显示数据量对数据进行切片显示。根据/index.html?p=4用GET方法获取页码

 1 def index(request):
 2     per_page_count=10 
 3     current_page=request.GET.get('p')
 4     current_page=int(current_page)
 5     start=(current_page-1)*per_page_count
 6     end=current_page*per_page_count
 7     data=USER_LIST[start:end]
 8 
 9     if current_page<=1:
10         prev_page=1
11         next_page = current_page + 1
12     else:
13         prev_page=current_page-1
14         next_page=current_page+1
15     return render(request,"index.html",{'user_list':data,'prev_page':prev_page,'next_page':next_page})
1 <body>
2 <ul>
3     {% for row in user_list %}
4         <li>{{ row.name }}-{{ row.age }}</li>
5     {% endfor %}
6 </ul>
7 <a href="/index.html?p={{ prev_page }}">上一页</a>
8 <a href="/index.html?p={{ next_page }}">下一页</a>
9 </body>

2.Django原生分页组件

Django封装的两个对象Paginator和page对象,通过输入的参数对数据进行处理得到各种属性,且两个对象互相包含。

Paginator对象 参数有全部数据的数组,以及每页显示条目数量,函数内部封装了对象的各项属性
# 全部数据:USER_LIST,=》得出共有多少条数据
# per_page: 每页显示条目数量
# count: 数据总个数
# num_pages:总页数
# page_range:总页数的索引范围,如: (1,10),(1,200)
# page: page对象(是否具有下一页;是否有上一页;)
Page对象 参数为当前的页码
# has_next 是否有下一页
# next_page_number 下一页页码
# has_previous 是否有上一页
# previous_page_number 上一页页码
# object_list 分页之后的数据列表,已经切片好的数据
# number 当前页
# paginator paginator对象
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
 1 def index1(request):
 2     current_page=request.GET.get('p')
 3     paginator=Paginator(USER_LIST, 10)
 4     try:
 5         posts=paginator.page(current_page)   
 6     except PageNotAnInteger:
 7         posts=paginator.page(1)
 8     except EmptyPage:
 9         posts=paginator.page(paginator.num_pages)
10     return render(request,"index1.html",{"posts":posts})
 1 <ul>
 2     {% for row in posts.object_list %}
 3         <li>{{ row.name }}-{{ row.age }}</li>
 4     {% endfor %}
 5 </ul>
 6 {% if posts.has_previous %}
 7 <a href="/index1.html?p={{ posts.previous_page_number }}">上一页</a>
 8 {% else %}
 9 <a href="#">上一页</a>
10 {% endif %}
11 {% for i in posts.paginator.pager_num_range %}
12     {% if i == posts.number  %}
13         <a style="font-size: 30px;" href="/index1.html?p={{ i }}">{{ i }}</a>
14     {% else %}
15         <a href="/index1.html?p={{ i }}">{{ i }}</a>
16     {% endif %}
17 {% endfor %}
18 {% if posts.has_next %}
19 <a href="/index1.html?p={{ posts.next_page_number }}">下一页</a>
20 {% endif %}
21 <span>
22     {{ posts.number }}/{{ posts.paginator.num_pages }}
23 </span>

可做出类似  上一页 下一页 1/100  的分页效果

3.自定义分页组件

通过继承原生分页组件自定制分页功能

 1 class CustomPaginator(Paginator):
 2     def __init__(self,current_page, per_pager_num,*args,**kwargs):
 3         # 当前页
 4         self.current_page = int(current_page)
 5         # 最多显示的页码数量 11
 6         self.per_pager_num = int(per_pager_num)
 7         super(CustomPaginator,self).__init__(*args,**kwargs)
 8     def pager_num_range(self):
 9         # 当前页
10         #self.current_page
11         # 最多显示的页码数量 11
12         #self.per_pager_num
13         # 总页数
14         # self.num_pages
15         if self.num_pages < self.per_pager_num:
16             return range(1,self.num_pages+1)
17         # 总页数特别多 5
18         part = int(self.per_pager_num/2)
19         if self.current_page <= part:
20             return range(1,self.per_pager_num+1)
21         if (self.current_page + part) > self.num_pages:
22             return range(self.num_pages-self.per_pager_num+1,self.num_pages+1)
23         return range(self.current_page-part,self.current_page+part+1)

效果如下:
上一页 1 2 3 4 5 6 7 8 9 10 11 下一页 6/100

4.bootstrap自定制样式

 1 {#bootstrap自定制样式#}
 2 <nav aria-label="Page navigation">
 3   <ul class="pagination">
 4     <li>
 5       {% if posts.has_previous %}
 6       <a href="/index1.html?p={{ posts.previous_page_number }}" aria-label="Previous">
 7         <span aria-hidden="true">&laquo;</span>
 8       </a>
 9        {% else %}
10         <a href="#" aria-label="Previous">
11         <span aria-hidden="true">&laquo;</span>
12       </a>
13         {% endif %}
14     </li>
15     {% for i in posts.paginator.pager_num_range %}
16     {% if i == posts.number  %}
17         <li class="active"><a  href="/index1.html?p={{ i }}">{{ i }}</a></li>
18     {% else %}
19         <li><a href="/index1.html?p={{ i }}">{{ i }}</a></li>
20     {% endif %}
21     {% endfor %}
22     <li>
23       <a href="/index1.html?p={{ posts.next_page_number }}" aria-label="Next">
24         <span aria-hidden="true">&raquo;</span>
25       </a>
26     </li>
27   </ul>
28 </nav>

 5.完全脱离Django内置模块的自写分页组件

 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='/index2.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='/index2.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='/index2.html?p=%s'>%s</a></li>" %(i,i)
69             else:
70                 temp = "<li><a href='/index2.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='/index2.html?p=%s'>下一页</a></li>" % (self.current_page + 1,)
77         page_list.append(nex)
78 
79         last = "<li><a href='/index2.html?p=%s'>尾页</a></li>" %(self.num_pages,)
80         page_list.append(last)
81 
82         return ''.join(page_list)

适用于各种框架


 

posted @ 2018-03-31 10:36  blue_day  阅读(232)  评论(0编辑  收藏  举报