Django框架實現(xiàn)的分頁demo示例
本文實例講述了Django框架實現(xiàn)的分頁。分享給大家供大家參考,具體如下:
首先初始化model,建表
class Book(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
class Meta:
db_table = 'books'
然后用pycharm的數(shù)據(jù)庫模塊可視化插入
分頁思路
url傳遞參數(shù)http://127.0.0.1:8000/books/?page=5比如這樣傳遞的參數(shù)就是5,就顯示第五頁,
1.get到所有圖書對象
2.計算好每一頁應該有幾個數(shù)據(jù)
3.根據(jù)不同的page值傳遞
def books(request):
#取從url傳遞的參數(shù)
page_num = request.GET.get('page')
page_num = int(page_num)
start = (page_num-1)*5
end = page_num*5
#總頁碼數(shù)是?
per_page = 5
total = models.Book.objects.all().count()
total,more =divmod(total,per_page)
if more:
total+=1
all_books = models.Book.objects.all()[start:end]
#自己拼接分頁的html代碼
html_str_list = []
for i in range(1,total):
tmp = '<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{}</li>'.format(i,i)
html_str_list.append(tmp)
page_html = "".join(html_str_list)
return render(request,'books.html',{'books':all_books,'total_page':total,'page_html':page_html})
拿到數(shù)據(jù)總量的值,每一頁的數(shù)量為5,如果有余數(shù)則total+1也就是增加一個頁面.
建立一個列表,去拼接a標簽,最后傳遞給前端
前端
前端的樣式用到了boottrap,可以直接看文檔.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>書記列表</title>
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.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.name }}</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>
{{ page_html|safe }}
<li>
<a href="#" rel="external nofollow" rel="external nofollow" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div>
</body>
</html>
{{ page_html|safe }}
傳遞過來的page_html要用safe過濾器,不然無法轉移成html.
最終效果

分頁優(yōu)化
設置一個首頁一個尾頁,以及顯示局部的頁面
def books(request):
# 取從url傳遞的參數(shù)
page_num = request.GET.get('page')
page_num = int(page_num)
start = (page_num - 1) * 5
end = page_num * 5
# 總頁碼數(shù)是?
per_page = 5
# 頁面上總共展示多少頁面
max_page = 11
half_max_page = max_page // 2
# 頁面上展示的頁面從哪開始
page_start = page_num - half_max_page
if page_start <= 1:
page_start = 1
total = models.Book.objects.all().count()
# 頁面到哪結束
page_end = page_num+half_max_page
if page_end > total:
page_end = total
page_start = total - max_page
total, more = divmod(total, per_page)
if more:
total += 1
all_books = models.Book.objects.all()[start:end]
# 自己拼接分頁的html代碼
html_str_list = []
html_str_list.append('<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</li>'.format(1,1))
for i in range(page_start, page_end+1):
tmp = '<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{}</li>'.format(i, i)
html_str_list.append(tmp)
html_str_list.append('<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >最后一頁</li>'.format(total))
page_html = "".join(html_str_list)
return render(request, 'books.html', {'books': all_books, 'total_page': total, 'page_html': page_html})
希望本文所述對大家基于Django框架的Python程序設計有所幫助。
相關文章
python中如何實現(xiàn)將數(shù)據(jù)分成訓練集與測試集的方法
這篇文章主要介紹了python中如何實現(xiàn)將數(shù)據(jù)分成訓練集與測試集的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
Python機器學習庫scikit-learn安裝與基本使用教程
這篇文章主要介紹了Python機器學習庫scikit-learn安裝與基本使用,較為詳細的介紹了機器學習庫scikit-learn的功能、原理、基本安裝與簡單使用方法,需要的朋友可以參考下2018-06-06

