13_分頁器
分頁器
使用方法:
# 匯入
from django.core.paginator import Paginator
# 在檢視函式中
def test(request):
book_list = Book.objects.all() # 所有書籍物件
# 例項化出一個物件,第一個引數是要分頁的書籍物件,第二個引數是每頁多少條資料
paginator = Paginator(book_list,10)
# -----------------------------物件內的屬性----------------------------
# 資料總條數--100條
print(paginator.count)
# 總頁數--10頁
print(paginator.num_pages)
# 頁碼數的列表
print(paginator.page_range)
# 取到第 x 頁 ,返回一個Page物件
current_page=paginator.page(5)
# 當前頁碼內所有的資料
print(current_page.object_list)
# 是否有下一頁
print(current_page.has_next())
# 是否有上一頁
print(current_page.has_previous())
# 下一頁頁碼數
print(current_page.next_page_number())
# 上一頁的頁碼數
print(current_page.previous_page_number())
簡單小案例:
檢視函式頁面
# views.py
def test(request):
book_list = models.Book.objects.all()
paginator = Paginator(book_list, 5)
try:
# 通過GET請求方式取出當前頁碼數
current_page_num = int(request.GET.get('page'))
# 通過當前頁碼數,返回一個page物件
current_page = paginator.page(current_page_num)
if paginator.num_pages > 11:
if current_page_num - 5 < 1:
page_range = range(1, 12)
elif current_page_num + 5 > paginator.num_pages:
page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
else:
page_range = range(current_page_num - 5, current_page_num + 6)
else:
page_range = paginator.page_range
except Exception as e:
current_page_num = 1
return render(request, 'test.html', locals())
html頁面
<!-- html頁面 -->
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<script src="/static/jquery-3.3.1.js"></script>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
<title>圖書</title>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<nav aria-label="Page navigation">
<ul class="pagination">
{% if current_page.has_previous %}
<li>
<a href="/test/?page={{ current_page.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">上一頁</span>
</a>
</li>
{% else %}
<li class="disabled">
<a href="" aria-label="Previous">
<span aria-hidden="true">上一頁</span>
</a>
</li>
{% endif %}
{% for foo in page_range %}
{% if current_page_num == foo %}
<li class="active"><a href="/test/?page={{ foo }}">{{ foo }}</a></li>
{% else %}
<li><a href="/test/?page={{ foo }}">{{ foo }}</a></li>
{% endif %}
{% endfor %}
{% if current_page.has_next %}
<li>
<a href="/test/?page={{ current_page.next_page_number }}" aria-label="Next">
<span aria-hidden="true">下一頁</span>
</a>
</li>
{% else %}
<li class="disabled">
<a href="" aria-label="Next">
<span aria-hidden="true">下一頁</span>
</a>
</li>
{% endif %}
</ul>
</nav>
<table class="table table-striped">
<thead>
<tr>
<th>書籍名</th>
<th>書籍價格</th>
</tr>
</thead>
<tbody>
{% for book in current_page %}
<tr>
<td>{{ book.name }}</td>
<td>{{ book.price }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
HTML頁面講解:
<table class="table table-striped">
<thead>
<tr>
<th>書籍名</th>
<th>書籍價格</th>
</tr>
</thead>
<tbody>
<!-- current_page 是當前頁所包含的書籍物件,從後臺傳過來 -->
{% for book in current_page %}
<tr>
<td>{{ book.name }}</td>
<td>{{ book.price }}</td>
</tr>
{% endfor %}
</tbody>
</table>
上下頁:
<!-- 下一頁與上一頁相同,判斷是否有下一頁 -->
<!-- 判斷是否有上一頁,如果沒有,禁用上一頁,並且將 a標籤 裡的 href 清空 -->
{% if current_page.has_previous %}
<li>
<a href="/test/?page={{ current_page.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">上一頁</span>
</a>
</li>
{% else %}
<li class="disabled">
<a href="" aria-label="Previous">
<span aria-hidden="true">上一頁</span>
</a>
</li>
{% endif %}
<!-- page_range指的是顯示區間,比如當前頁為9,顯示左5右5,顯示區間就是range(4,15),顧頭不顧尾 -->
{% for foo in page_range %}
<!-- 如果當前頁數與遍歷出的頁數相等,則該頁數為活躍狀態,新增 active -->
{% if current_page_num == foo %}
<li class="active"><a href="/test/?page={{ foo }}">{{ foo }}</a></li>
{% else %}
<li><a href="/test/?page={{ foo }}">{{ foo }}</a></li>
{% endif %}
{% endfor %}
==================================================================
分頁器模板
<nav aria-label="Page navigation">
<ul class="pagination">
{% if current_page.has_previous %}
<li>
<a href="/?page={{ current_page.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">上一頁</span>
</a>
</li>
{% else %}
<li class="disabled">
<a href="" aria-label="Previous">
<span aria-hidden="true">上一頁</span>
</a>
</li>
{% endif %}
{% for foo in page_range %}
{% if current_page_num == foo %}
<li class="active"><a href="/?page={{ foo }}">{{ foo }}</a></li>
{% else %}
<li><a href="/?page={{ foo }}">{{ foo }}</a></li>
{% endif %}
{% endfor %}
{% if current_page.has_next %}
<li>
<a href="/?page={{ current_page.next_page_number }}" aria-label="Next">
<span aria-hidden="true">下一頁</span>
</a>
</li>
{% else %}
<li class="disabled">
<a href="" aria-label="Next">
<span aria-hidden="true">下一頁</span>
</a>
</li>
{% endif %}
</ul>
</nav>
後臺程式碼
# 分頁器
paginator = Paginator(article_list, 3)
try:
current_page_num = int(request.GET.get('page'))
current_page = paginator.page(current_page_num)
except Exception as e:
current_page_num = 1
current_page = paginator.page(current_page_num)
if paginator.num_pages > 11:
if current_page_num - 5 < 1:
page_range = range(1, 12)
elif current_page_num + 5 > paginator.num_pages:
page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
else:
page_range = range(current_page_num - 5, current_page_num + 6)
else:
page_range = paginator.page_range
相關文章
- 分頁器
- Django(68)drf分頁器的使用Django
- mysql分頁-limit offset分頁MySqlMIT
- 5 個最酷的終端分頁器
- Mybatis 分頁:Pagehelper + 攔截器實現MyBatis
- flask 分頁 | 翻頁Flask
- php 分頁 分頁類 簡單實用PHP
- 面向Vue新人:用Vue寫一個分頁器Vue
- Flask——分頁Flask
- 集合分頁
- php 分頁PHP
- 分頁案例
- layui 資料表格重新載入資料分頁器回到第一頁UI
- 13_深入解析Oracle資料庫bootstrapOracle資料庫boot
- Lavarel Ajax 分頁時 獲取分頁資訊
- [Django高階之批量插入資料、分頁器元件]Django元件
- 【C】 13_接續符和轉義符
- REST framework:分頁RESTFramework
- 如何高效分頁
- 日誌、分頁
- 分段與分頁
- php分頁類PHP
- ES 分頁方案
- Django的分頁Django
- jq寫分頁
- Spring JPA 分頁Spring
- 讀寫分離 & 分庫分表 & 深度分頁
- Spring專案處理分頁(邏輯和物理分頁)Spring
- Flutter初探 上下拉分頁請求+計算器實現Flutter
- 分庫分表後的分頁查詢
- WebMagic抓取 table分頁資料, table分頁時,URL不變Web
- 分頁實現前臺後臺不同效果,分頁類引入
- 分頁練習-網頁開發常用網頁
- SqlServer 分頁學習SQLServer
- flask_sqlalchemy 分頁FlaskSQL
- MongoDB 的分頁(Pagination)MongoDB
- MySQL、Elasticsearch 深度分頁MySqlElasticsearch
- yii2-分頁