本文和大家分享的主要是django
分頁組件相關(guān)內(nèi)容,希望通過本文的分享對剛
入門python的童鞋有所幫助。
1.
設(shè)計思路
根據(jù)每頁顯示數(shù)據(jù)量對數(shù)據(jù)進行切片顯示。根據(jù)/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
對象,通過輸入的參數(shù)對數(shù)據(jù)進行處理得到各種屬性,且兩個對象互相包含。
Paginator
對象 參數(shù)有全部數(shù)據(jù)的數(shù)組,以及每頁顯示條目數(shù)量
,
函數(shù)內(nèi)部封裝了對象的各項屬性
#
全部數(shù)據(jù):
USER_LIST
,
=
》得出共有多少條數(shù)據(jù)
# per_page:
每頁顯示條目數(shù)量
# count:
數(shù)據(jù)總個數(shù)
# num_pages:
總頁數(shù)
# page_range:
總頁數(shù)的索引范圍,如
: (1,10),(1,200)
# page: page
對象(是否具有下一頁;是否有上一頁;)
Page
對象 參數(shù)為當(dāng)前的頁碼
# has_next
是否有下一頁
# next_page_number
下一頁頁碼
# has_previous
是否有上一頁
# previous_page_number
上一頁頁碼
# object_list
分頁之后的數(shù)據(jù)列表,已經(jīng)切片好的數(shù)據(jù)
# number
當(dāng)前頁
# 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 #
當(dāng)前頁
4 self.current_page = int(current_page)
5 #
最多顯示的頁碼數(shù)量
11
6 self.per_pager_num = int(per_pager_num)
7 super(CustomPaginator,self).__init__(*args,**kwargs)
8 def pager_num_range(self):
9 #
當(dāng)前頁
10 #self.current_page
11 #
最多顯示的頁碼數(shù)量
11
12 #self.per_pager_num
13 #
總頁數(shù)
14 # self.num_pages
15 if self.num_pages < self.per_pager_num:
16 return range(1,self.num_pages+1)
17 #
總頁數(shù)特別多
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">?</span>
8 </a>
9 {% else %}
10 <a href="#" aria-label="Previous">
11 <span aria-hidden="true">?</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">?</span>
25 </a>
26 </li>
27 </ul>
28 </nav>