Python Django 封裝分頁(yè)成通用的模塊詳解
這篇文章主要介紹了Python Django 封裝分頁(yè)成通用的模塊詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
新建 utils 文件夾,并創(chuàng)建 page.py

page.py:
class ShowPage(object):
def __init__(self, page_num, total_count, url_prefix, per_page=10, max_page=11):
'''
:param page_num: 當(dāng)前頁(yè)碼數(shù)
:param total_count: 數(shù)據(jù)總數(shù)
:param url_prefix: a 標(biāo)簽 href 的前綴
:param per_page: 每頁(yè)展示的數(shù)據(jù)數(shù)
:param max_page: 頁(yè)面上最多顯示的頁(yè)碼數(shù)
'''
self.url_prefix = url_prefix
self.max_page = max_page
# 總共需要多少頁(yè)碼來(lái)顯示
total_page, m = divmod(total_count, per_page)
# 如果還有數(shù)據(jù)
if m:
total_page += 1
self.total_page = total_page
try:
page_num = int(page_num)
# 如果輸入的頁(yè)碼數(shù)超過了最大的頁(yè)碼數(shù),默認(rèn)返回最后一頁(yè)
if page_num > self.total_page:
page_num = self.total_page
# 如果輸入的頁(yè)碼數(shù)小于 1,則返回第一頁(yè)
if page_num < 1:
page_num = 1
except Exception as e:
# 當(dāng)輸入的頁(yè)碼不是正經(jīng)數(shù)字的時(shí)候 默認(rèn)返回第一頁(yè)的數(shù)據(jù)
page_num = 1
self.page_num = page_num
# 定義兩個(gè)變量保存數(shù)據(jù)從哪兒取到哪兒
self.data_start = (self.page_num - 1) * 10
self.data_end = self.page_num * 10
# 頁(yè)面上總共展示多少頁(yè)碼
if self.total_page < self.max_page:
self.max_page = self.total_page
half_max_page = self.max_page // 2
# 頁(yè)面上展示的頁(yè)碼的開始頁(yè)
page_start = self.page_num - half_max_page
# 頁(yè)面上展示的頁(yè)碼的結(jié)束頁(yè)
page_end = self.page_num + half_max_page
# 如果當(dāng)前頁(yè)減一半比 1 還小
if page_start <= 1:
page_start = 1
page_end = self.max_page
# 如果當(dāng)前頁(yè)加一半比總頁(yè)碼還大
if page_end >= self.total_page:
page_end = self.total_page
page_start = self.total_page - self.max_page + 1
self.page_start = page_start
self.page_end = page_end
@property
def start(self):
return self.data_start
@property
def end(self):
return self.data_end
def page_html(self):
# 拼接 html 的分頁(yè)代碼
html_list = []
# 添加首頁(yè)按鈕
html_list.append('<li><a href="{}?page=1" rel="external nofollow" >首頁(yè)</a></li>'.format( self.url_prefix))
# 如果是第一頁(yè),就沒有上一頁(yè)
if self.page_num <= 1:
html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(self.page_num - 1))
else:
# 加一個(gè)上一頁(yè)的標(biāo)簽
html_list.append('<li><a href="{}?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(self.url_prefix, self.page_num-1))
# 展示的頁(yè)碼
for i in range(self.page_start, self.page_end + 1):
# 給當(dāng)前頁(yè)添加 active
if i == self.page_num:
tmp = '<li class="active"><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" >{1}</a></li>'.format(self.url_prefix, i)
else:
tmp = '<li><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" >{1}</a></li>'.format(self.url_prefix, i)
html_list.append(tmp)
# 如果是最后一頁(yè),就沒有下一頁(yè)
if self.page_num >= self.total_page:
html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>')
else:
html_list.append(
'<li><a href="{}?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>'.format(self.url_prefix, self.page_num + 1))
# 添加尾頁(yè)按鈕
html_list.append('<li><a href="{}?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a></li>'.format(self.url_prefix, self.total_page))
page_html = "".join(html_list) # 拼接 html 的分頁(yè)代碼
return page_html
views.py:
from django.shortcuts import render
from app01 import models
def book_list(request):
# 從URL取參數(shù)
page_num = request.GET.get("page")
print(page_num, type(page_num))
# 書籍總數(shù)
total_count = models.Book.objects.all().count()
# 導(dǎo)入顯示頁(yè)碼的函數(shù)
from utils.page import ShowPage
page_obj = ShowPage(page_num, total_count, per_page=10, url_prefix="/book_list/", max_page=11, )
ret = models.Book.objects.all()[page_obj.start:page_obj.end]
print(ret)
page_html = page_obj.page_html()
return render(request, "book_list.html", {"books": ret, "page_html": page_html})
book_list.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>書籍列表</title>
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css" rel="external nofollow" >
</head>
<body>
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>序號(hào)</th>
<th>id</th>
<th>書名</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book.id }}</td>
<td>{{ book.title }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
{{ page_html|safe }}
</li>
</ul>
</nav>
</div>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python web框架中實(shí)現(xiàn)原生分頁(yè)
- Python Django實(shí)現(xiàn)layui風(fēng)格+django分頁(yè)功能的例子
- Python Django 簡(jiǎn)單分頁(yè)的實(shí)現(xiàn)代碼解析
- 詳解Python odoo中嵌入html簡(jiǎn)單的分頁(yè)功能
- python 實(shí)現(xiàn)分頁(yè)顯示從es中獲取的數(shù)據(jù)方法
- python flask實(shí)現(xiàn)分頁(yè)的示例代碼
- python實(shí)現(xiàn)分頁(yè)效果
- Python利用flask sqlalchemy實(shí)現(xiàn)分頁(yè)效果
- python flask實(shí)現(xiàn)分頁(yè)效果
- Python+Selenium自動(dòng)化實(shí)現(xiàn)分頁(yè)(pagination)處理
- Python的Flask框架中實(shí)現(xiàn)分頁(yè)功能的教程
- python使用BeautifulSoup分頁(yè)網(wǎng)頁(yè)中超鏈接的方法
- python Django框架實(shí)現(xiàn)web端分頁(yè)呈現(xiàn)數(shù)據(jù)
相關(guān)文章
python對(duì)指定目錄下文件進(jìn)行批量重命名的方法
這篇文章主要介紹了python對(duì)指定目錄下文件進(jìn)行批量重命名的方法,涉及Python中replace及join方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Python使用QQ郵箱發(fā)送郵件實(shí)例與QQ郵箱設(shè)置詳解
這篇文章主要介紹了Python發(fā)送QQ郵件實(shí)例與QQ郵箱設(shè)置詳解,需要的朋友可以參考下2020-02-02
python實(shí)現(xiàn)簡(jiǎn)單登陸系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單登陸系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Python坐標(biāo)軸操作及設(shè)置代碼實(shí)例
這篇文章主要介紹了Python坐標(biāo)軸操作及設(shè)置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

