溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python Django 封裝分頁成通用的模塊詳解

發(fā)布時間:2020-10-19 15:17:03 來源:腳本之家 閱讀:128 作者:Sch01aR# 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Python Django 封裝分頁成通用的模塊詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

新建 utils 文件夾,并創(chuàng)建 page.py

Python Django 封裝分頁成通用的模塊詳解

page.py:

class ShowPage(object):
  def __init__(self, page_num, total_count, url_prefix, per_page=10, max_page=11):
    '''
    :param page_num: 當(dāng)前頁碼數(shù)
    :param total_count: 數(shù)據(jù)總數(shù)
    :param url_prefix: a 標(biāo)簽 href 的前綴
    :param per_page: 每頁展示的數(shù)據(jù)數(shù)
    :param max_page: 頁面上最多顯示的頁碼數(shù)
    '''
    self.url_prefix = url_prefix
    self.max_page = max_page
    # 總共需要多少頁碼來顯示
    total_page, m = divmod(total_count, per_page) 
    # 如果還有數(shù)據(jù)
    if m:
      total_page += 1
    self.total_page = total_page 
    try:
      page_num = int(page_num)
      # 如果輸入的頁碼數(shù)超過了最大的頁碼數(shù),默認(rèn)返回最后一頁
      if page_num > self.total_page:
        page_num = self.total_page
      # 如果輸入的頁碼數(shù)小于 1,則返回第一頁
      if page_num < 1:
        page_num = 1
    except Exception as e:
      # 當(dāng)輸入的頁碼不是正經(jīng)數(shù)字的時候 默認(rèn)返回第一頁的數(shù)據(jù)
      page_num = 1
    self.page_num = page_num 
    # 定義兩個變量保存數(shù)據(jù)從哪兒取到哪兒
    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
    # 頁面上展示的頁碼的結(jié)束頁
    page_end = self.page_num + half_max_page
    # 如果當(dāng)前頁減一半比 1 還小
    if page_start <= 1:
      page_start = 1
      page_end = self.max_page
    # 如果當(dāng)前頁加一半比總頁碼還大
    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:
      # 加一個上一頁的標(biāo)簽
      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):
      # 給當(dāng)前頁添加 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取參數(shù)
  page_num = request.GET.get("page")
  print(page_num, type(page_num)) 
  # 書籍總數(shù)
  total_count = models.Book.objects.all().count()
  # 導(dǎo)入顯示頁碼的函數(shù)
  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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI