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.計算好每一頁應(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è)計有所幫助。
相關(guān)文章
Pytorch學(xué)習(xí)筆記DCGAN極簡入門教程
網(wǎng)上GAN的教程太多了,這邊也談一下自己的理解,本文給大家介紹一下GAN的兩部分組成,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09python中如何實現(xiàn)將數(shù)據(jù)分成訓(xùn)練集與測試集的方法
這篇文章主要介紹了python中如何實現(xiàn)將數(shù)據(jù)分成訓(xùn)練集與測試集的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Python機器學(xué)習(xí)庫scikit-learn安裝與基本使用教程
這篇文章主要介紹了Python機器學(xué)習(xí)庫scikit-learn安裝與基本使用,較為詳細的介紹了機器學(xué)習(xí)庫scikit-learn的功能、原理、基本安裝與簡單使用方法,需要的朋友可以參考下2018-06-06