您好,登錄后才能下訂單哦!
這篇文章主要介紹了Python Django 封裝分頁成通用的模塊詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
新建 utils 文件夾,并創建 page.py
page.py:
class ShowPage(object): def __init__(self, page_num, total_count, url_prefix, per_page=10, max_page=11): ''' :param page_num: 當前頁碼數 :param total_count: 數據總數 :param url_prefix: a 標簽 href 的前綴 :param per_page: 每頁展示的數據數 :param max_page: 頁面上最多顯示的頁碼數 ''' self.url_prefix = url_prefix self.max_page = max_page # 總共需要多少頁碼來顯示 total_page, m = divmod(total_count, per_page) # 如果還有數據 if m: total_page += 1 self.total_page = total_page try: page_num = int(page_num) # 如果輸入的頁碼數超過了最大的頁碼數,默認返回最后一頁 if page_num > self.total_page: page_num = self.total_page # 如果輸入的頁碼數小于 1,則返回第一頁 if page_num < 1: page_num = 1 except Exception as e: # 當輸入的頁碼不是正經數字的時候 默認返回第一頁的數據 page_num = 1 self.page_num = page_num # 定義兩個變量保存數據從哪兒取到哪兒 self.data_start = (self.page_num - 1) * 10 self.data_end = self.page_num * 10 # 頁面上總共展示多少頁碼 if self.total_page < self.max_page: self.max_page = self.total_page half_max_page = self.max_page // 2 # 頁面上展示的頁碼的開始頁 page_start = self.page_num - half_max_page # 頁面上展示的頁碼的結束頁 page_end = self.page_num + half_max_page # 如果當前頁減一半比 1 還小 if page_start <= 1: page_start = 1 page_end = self.max_page # 如果當前頁加一半比總頁碼還大 if page_end >= self.total_page: page_end = self.total_page page_start = self.total_page - self.max_page + 1 self.page_start = page_start self.page_end = page_end @property def start(self): return self.data_start @property def end(self): return self.data_end def page_html(self): # 拼接 html 的分頁代碼 html_list = [] # 添加首頁按鈕 html_list.append('<li><a href="{}?page=1" rel="external nofollow" >首頁</a></li>'.format( self.url_prefix)) # 如果是第一頁,就沒有上一頁 if self.page_num <= 1: html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(self.page_num - 1)) else: # 加一個上一頁的標簽 html_list.append('<li><a href="{}?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(self.url_prefix, self.page_num-1)) # 展示的頁碼 for i in range(self.page_start, self.page_end + 1): # 給當前頁添加 active if i == self.page_num: tmp = '<li class="active"><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" >{1}</a></li>'.format(self.url_prefix, i) else: tmp = '<li><a href="{0}?page={1}" rel="external nofollow" rel="external nofollow" >{1}</a></li>'.format(self.url_prefix, i) html_list.append(tmp) # 如果是最后一頁,就沒有下一頁 if self.page_num >= self.total_page: html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>') else: html_list.append( '<li><a href="{}?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>'.format(self.url_prefix, self.page_num + 1)) # 添加尾頁按鈕 html_list.append('<li><a href="{}?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a></li>'.format(self.url_prefix, self.total_page)) page_html = "".join(html_list) # 拼接 html 的分頁代碼 return page_html
views.py:
from django.shortcuts import render from app01 import models def book_list(request): # 從URL取參數 page_num = request.GET.get("page") print(page_num, type(page_num)) # 書籍總數 total_count = models.Book.objects.all().count() # 導入顯示頁碼的函數 from utils.page import ShowPage page_obj = ShowPage(page_num, total_count, per_page=10, url_prefix="/book_list/", max_page=11, ) ret = models.Book.objects.all()[page_obj.start:page_obj.end] print(ret) page_html = page_obj.page_html() return render(request, "book_list.html", {"books": ret, "page_html": page_html})
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> {{ page_html|safe }} </li> </ul> </nav> </div> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。