Django框架實(shí)現(xiàn)的分頁demo示例
本文實(shí)例講述了Django框架實(shí)現(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.計算好每一頁應(yīng)該有幾個數(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標(biāo)簽,最后傳遞給前端
前端
前端的樣式用到了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過濾器,不然無法轉(zhuǎn)移成html.
最終效果

分頁優(yōu)化
設(shè)置一個首頁一個尾頁,以及顯示局部的頁面
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()
# 頁面到哪結(jié)束
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程序設(shè)計有所幫助。
- django實(shí)現(xiàn)分頁的方法
- Django實(shí)現(xiàn)快速分頁的方法實(shí)例
- Django自定義分頁與bootstrap分頁結(jié)合
- Django實(shí)現(xiàn)簡單分頁功能的方法詳解
- Django原生sql也能使用Paginator分頁的示例代碼
- Django自定義分頁效果
- Django分頁查詢并返回jsons數(shù)據(jù)(中文亂碼解決方法)
- 在django中使用自定義標(biāo)簽實(shí)現(xiàn)分頁功能
- django js實(shí)現(xiàn)部分頁面刷新的示例代碼
- django之session與分頁(實(shí)例講解)
- Django如何自定義分頁
相關(guān)文章
Python設(shè)計模式之解釋器模式原理與用法實(shí)例分析
這篇文章主要介紹了Python設(shè)計模式之解釋器模式原理與用法,結(jié)合具體實(shí)例形式分析了解釋器模式的概念、原理、定義及使用方法,需要的朋友可以參考下2019-01-01
Effective Python bytes 與 str 的區(qū)別
這篇文章主要介紹了Effective Python bytes 與 str 的區(qū)別,Python 有兩種類型可以表示字符序列,下面圍繞Python bytes 與 str 的相關(guān)資料展開內(nèi)容,需要的朋友可以參考一下2021-11-11
Python關(guān)鍵字之global與nonlocal
這篇文章主要為大家詳細(xì)介紹了Python關(guān)鍵字之global與nonlocal,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
Python數(shù)字圖像處理代數(shù)之加減乘運(yùn)算
這篇文章主要介紹了Python數(shù)字圖像處理代數(shù)運(yùn)算,對其中的加、減、乘運(yùn)算分別作了詳細(xì)的講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
python中while和for的區(qū)別總結(jié)
在本篇內(nèi)容里小編給大家分享的是關(guān)于python中while和for的區(qū)別以及相關(guān)知識點(diǎn),需要的朋友們可以學(xué)習(xí)下。2019-06-06
python在屏幕上點(diǎn)擊特定按鈕或圖像效果實(shí)例
這篇文章主要給大家介紹了關(guān)于python在屏幕上點(diǎn)擊特定按鈕或圖像效果的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-09-09
Python使用ctypes調(diào)用C/C++的方法
今天小編就為大家分享一篇關(guān)于Python使用ctypes調(diào)用C/C++的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
感知器基礎(chǔ)原理及python實(shí)現(xiàn)過程詳解
這篇文章主要介紹了感知器基礎(chǔ)原理及python實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09

