Django自定義分頁效果
分頁功能在每個網(wǎng)站都是必要的,對于分頁來說,其實就是根據(jù)用戶的輸入計算出應(yīng)該顯示在頁面上的數(shù)據(jù)在數(shù)據(jù)庫表中的起始位置。
確定分頁需求:
1. 每頁顯示的數(shù)據(jù)條數(shù)
2. 每頁顯示頁號鏈接數(shù)
3. 上一頁和下一頁
4. 首頁和末頁
效果圖:

首先,利用django內(nèi)置的分頁功能,寫分頁類:
from django.core.paginator import Paginator, Page # 導(dǎo)入django分頁模塊
class PageInfo(object):
def __init__(self, current_page, all_count, base_url, per_page=10, show_page=11):
"""
:param current_page: 當(dāng)前頁
:param all_count: 總頁數(shù)
:param base_url: 模板
:param per_page: 每頁顯示數(shù)據(jù)條數(shù)
:param show_page: 顯示鏈接頁個數(shù)
"""
#若url錯誤,默認顯示第一頁(錯誤類型可能為:空頁面編號,非整數(shù)型頁面編號)
try:
self.current_page = int(current_page)
except Exception as e:
self.current_page = 1
#根據(jù)數(shù)據(jù)庫信息條數(shù)得出總頁數(shù)
a, b = divmod(all_count, per_page)
if b:
a += 1
self.all_page = a
self.base_url = base_url
self.per_page = per_page
self.show_page = show_page
#當(dāng)前頁起始數(shù)據(jù)id
def start_data(self):
return (self.current_page - 1) * self.per_page
#當(dāng)前頁結(jié)束數(shù)據(jù)id
def end_data(self):
return self.current_page * self.per_page
#動態(tài)生成前端html
def pager(self):
page_list = []
half = int((self.show_page - 1)/2)
#如果:總頁數(shù) < show_page,默認顯示頁數(shù)范圍為: 1~總頁數(shù)
if self.all_page < self.show_page:
start_page = 1
end_page = self.all_page + 1
#如果:總頁數(shù) > show_page
else:
#如果:current_page - half <= 0,默認顯示頁數(shù)范圍為:1~show_page
if self.current_page <= half:
start_page = 1
end_page = self.show_page + 1
else:
#如果:current_page + half >總頁數(shù),默認顯示頁數(shù)范圍為:總頁數(shù) - show_page ~ 總頁數(shù)
if self.current_page + half > self.all_page:
end_page = self.all_page + 1
start_page = end_page - self.show_page
else:
start_page = self.current_page - half
end_page = self.current_page + half + 1
#首頁
first_page = "<li><a href='%s?page=%s'>首頁</a></li>" %(self.base_url, 1)
page_list.append(first_page)
#上一頁(若當(dāng)前頁等于第一頁,則上一頁無鏈接,否則鏈接為當(dāng)前頁減1)
if self.current_page <= 1:
prev_page = "<li><a href='#'>上一頁</a></li>"
else:
prev_page = "<li><a href='%s?page=%s'>上一頁</a></li>" %(self.base_url, self.current_page-1)
page_list.append(prev_page)
#動態(tài)生成中間頁數(shù)鏈接
for i in range(start_page, end_page):
if i == self.current_page:
temp = "<li class='active'><a href='%s?page=%s'>%s</a></li>" %(self.base_url, i, i)
else:
temp = "<li><a href='%s?page=%s'>%s</a></li>" % (self.base_url, i, i)
page_list.append(temp)
#下一頁(若當(dāng)前頁等于最后頁,則下一頁無鏈接,否則鏈接為當(dāng)前頁加1)
if self.current_page >= self.all_page:
next_page = "<li><a href='#'>下一頁</a></li>"
else:
next_page = "<li><a href='%s?page=%s'>下一頁</a></li>" %(self.base_url, self.current_page+1)
page_list.append(next_page)
#末頁(若總頁數(shù)只有一頁,則無末頁標(biāo)簽)
if self.all_page > 1:
last_page = "<li><a href='%s?page=%s'>末頁</a></li>" % (self.base_url, self.all_page)
page_list.append(last_page)
return ''.join(page_list)
然后,在views中寫方法(此處寫在app01中):
from utils.pagnition import PageInfo # 從文件中導(dǎo)入上步自定義的分頁模塊
def custom(request):
all_count = models.UserInfo.objects.all().count()
# 獲取要顯示數(shù)據(jù)庫的總數(shù)據(jù)條數(shù)
page_info = PageInfo(request.GET.get('page'), all_count, '/custom.html/',)
# 生成分頁對象
user_list = models.UserInfo.objects.all()[page_info.start_data():page_info.end_data()]
# 利用分頁對象獲取當(dāng)前頁顯示數(shù)據(jù)
return render(request, 'custom.html', {'user_list': user_list, 'page_info': page_info})
# 模板渲染
然后,在templates目錄下寫“custom.html"文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>customers</title>
{# 引入bootstrap樣式#}
<link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css">
</head>
<body>
<h1>customers</h1>
{#當(dāng)前頁顯示的數(shù)據(jù)#}
<ul>
{% for row in user_list %}
<li>{{ row.name }}</li>
{% endfor %}
</ul>
{#分頁#}
<nav aria-label="Page navigation">
<ul class="pagination">
{# 傳入page_info.pager#}
{{ page_info.pager|safe }}
</ul>
</nav>
</body>
</html>
最后,新增url關(guān)系(urls.py):
from django.conf.urls import url from django.contrib import admin from app01 import views as app01_views urlpatterns = [ url(r'^custom.html/$', app01_views.custom), ]
至此,就完成了利用django的分頁功能自定義分頁模塊,可以應(yīng)用在不同的業(yè)務(wù)頁面上。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解讀殘差網(wǎng)絡(luò)(Residual Network),殘差連接(skip-connect)
這篇文章主要介紹了殘差網(wǎng)絡(luò)(Residual Network),殘差連接(skip-connect),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
關(guān)于adfuller函數(shù)返回值的參數(shù)說明與記錄
這篇文章主要介紹了關(guān)于adfuller函數(shù)返回值的參數(shù)說明與記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
TensorFlow繪制loss/accuracy曲線的實例
今天小編就為大家分享一篇TensorFlow繪制loss/accuracy曲線的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python標(biāo)準(zhǔn)庫中的sys你了解嗎
這篇文章主要為大家詳細介紹了Python標(biāo)準(zhǔn)庫中的sys,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

