Python Django 頁面上展示固定的頁碼數(shù)實(shí)現(xiàn)代碼
如果頁數(shù)太多的話,全部顯示在頁面上就會顯得很冗雜

可以在頁面中顯示規(guī)定的頁碼數(shù)
例如:

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>序號</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>
<a href="#" rel="external nofollow" rel="external nofollow" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li>
{{ page_html|safe }}
</li>
<li>
<a href="#" rel="external nofollow" rel="external nofollow" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div>
</body>
</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))
page_num = int(page_num)
# 定義兩個變量保存數(shù)據(jù)從哪兒取到哪兒
data_start = (page_num-1)*10
data_end = page_num*10
# 書籍總數(shù)
total_count = models.Book.objects.all().count()
# 每一頁顯示多少條數(shù)據(jù)
per_page = 10
# 總共需要多少頁碼來顯示
total_page, m = divmod(total_count, per_page)
# 頁面上最多展示的頁碼
max_page = 11
half_max_page = max_page // 2
# 頁面上展示的頁碼的開始頁
page_start = page_num - half_max_page
# 頁面上展示的頁碼的結(jié)束頁
page_end = page_num + half_max_page
# 如果當(dāng)前頁減一半比 1 小
if page_start <= 1:
page_start = 1
page_end = max_page
# 如果當(dāng)前頁加一半比總頁碼還大
if page_end > total_page:
page_end = total_page
page_start = total_page - max_page + 1
# 如果還有數(shù)據(jù)
if m:
total_page += 1
all_book = models.Book.objects.all()[data_start:data_end]
# 拼接 html 的分頁代碼
html_list = []
for i in range(page_start, page_end+1):
tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" >{0}</a></li>'.format(i)
html_list.append(tmp)
page_html = "".join(html_list)
return render(request, "book_list.html", {"books": all_book, "page_html": page_html})
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python3.8官網(wǎng)文檔之類的基礎(chǔ)語法閱讀
類提供了一種組合數(shù)據(jù)和功能的方法,今天通過本文給大家分享Python3.8官網(wǎng)文檔之類的基礎(chǔ)語法閱讀知識,感興趣的朋友跟隨小編一起看看吧2021-09-09
基于Python實(shí)現(xiàn)體育彩票選號器功能代碼實(shí)例
這篇文章主要介紹了基于Python實(shí)現(xiàn)體育彩票選號器功能代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
python處理json字符串(使用json.loads而不是eval())
eval 跟json.loads 是不一樣的函數(shù),是有實(shí)現(xiàn)不一樣功能的地方,但是在某些地方它們兩個函數(shù)的功能是一樣的,本文就詳細(xì)介紹一下2021-09-09
Python實(shí)現(xiàn)aes加密解密多種方法解析
這篇文章主要介紹了Python實(shí)現(xiàn)aes加密解密多種方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
Python實(shí)現(xiàn)實(shí)時跟隨微信窗口移動的GUI界面
Python寫一些簡單的GUI界面也是非常簡單的,并且Python有著豐富的庫,這些庫可以很方便我們?nèi)ゲ僮鱓indows系統(tǒng)。本文就來用Python編寫一個實(shí)時跟隨微信窗口移動的GUI界面吧2023-04-04
python爬取天氣數(shù)據(jù)的實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python爬取天氣數(shù)據(jù)的實(shí)例詳解內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2020-11-11
python 使用paramiko模塊進(jìn)行封裝,遠(yuǎn)程操作linux主機(jī)的示例代碼
這篇文章主要介紹了python 使用paramiko模塊進(jìn)行封裝,遠(yuǎn)程操作linux主機(jī)的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12

