Python Django 封裝分頁(yè)成通用的模塊詳解
這篇文章主要介紹了Python Django 封裝分頁(yè)成通用的模塊詳解,文中通過(guò)示例代碼介紹的非常詳細(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ù)超過(guò)了最大的頁(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è)碼的開(kāi)始頁(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è),就沒(méi)有上一頁(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è),就沒(méi)有下一頁(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ū)籍總數(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>書(shū)籍列表</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>書(shū)名</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內(nèi)存動(dòng)態(tài)分配過(guò)程詳解
這篇文章主要介紹了python內(nèi)存動(dòng)態(tài)分配過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Django使用httpresponse返回用戶(hù)頭像實(shí)例代碼
這篇文章主要介紹了Django使用httpresponse返回用戶(hù)頭像實(shí)例代碼2018-01-01
解決Tensorboard可視化錯(cuò)誤:不顯示數(shù)據(jù) No scalar data was found
今天小編就為大家分享一篇解決Tensorboard可視化錯(cuò)誤:不顯示數(shù)據(jù) No scalar data was found,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
python學(xué)習(xí)-List移除某個(gè)值remove和統(tǒng)計(jì)值次數(shù)count
這篇文章主要介紹了?python學(xué)習(xí)-List移除某個(gè)值remove和統(tǒng)計(jì)值次數(shù)count,文章基于python的相關(guān)內(nèi)容展開(kāi)詳細(xì)介紹,需要的小伙伴可以參考一下2022-04-04
pytorch中節(jié)約顯卡內(nèi)存的方法和技巧
顯存不足是很多人感到頭疼的問(wèn)題,畢竟能擁有大量顯存的實(shí)驗(yàn)室還是少數(shù),而現(xiàn)在的模型已經(jīng)越跑越大,模型參數(shù)量和數(shù)據(jù)集也越來(lái)越大,所以這篇文章給大家總結(jié)了一些pytorch中節(jié)約顯卡內(nèi)存的方法和技巧,需要的朋友可以參考下2023-11-11
基于Python實(shí)現(xiàn)Windows桌面定時(shí)提醒休息程序
這篇文章為大家詳細(xì)主要介紹了如何基于Python實(shí)現(xiàn)簡(jiǎn)單的Windows桌面定時(shí)提醒休息程序,文中的示例代碼講解詳細(xì),有需要的可以參考一下2024-11-11

