溫馨提示×

溫馨提示×

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

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

Django自定義分頁的示例

發(fā)布時間:2021-02-07 13:47:49 來源:億速云 閱讀:136 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Django自定義分頁的示例的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

Django自定義分頁的具體代碼,具體內(nèi)容如下

穩(wěn)扎穩(wěn)打版

def book(request):
 # 從URL取參數(shù)(訪問的頁碼)
 page_num = request.GET.get("page")
 try:
 # 將取出的page轉(zhuǎn)換為int類型
 page_num = int(page_num)
 except Exception as e:
 # 當(dāng)輸入的頁碼不是正經(jīng)數(shù)字的時候 默認(rèn)返回第一頁的數(shù)據(jù)
 page_num = 1

 # 數(shù)據(jù)庫總數(shù)據(jù)是多少條
 total_count = models.Book.objects.all().count()

 # 每一頁顯示多少條數(shù)據(jù)
 per_page = 10

 # 總共需要多少頁碼來展示
 total_page, m = divmod(total_count, per_page)
 if m:
 total_page += 1

 # 如果輸入的頁碼數(shù)超過了最大的頁碼數(shù),默認(rèn)返回最后一頁
 if page_num > total_page:
 page_num = total_page

 # 定義兩個變量從哪里開始到哪里結(jié)束
 data_start = (page_num - 1) * 10
 data_end = page_num * 10

 # 頁面上總共展示多少頁碼
 max_page = 11
 if total_page < max_page:
 max_page = total_page

 # 把從URL中獲取的page_num 當(dāng)做是顯示頁面的中間值, 那么展示的便是當(dāng)前page_num 的前五頁和后后五頁
 half_max_page = max_page // 2
 # 根據(jù)展示的總頁碼算出頁面上展示的頁碼從哪兒開始
 page_start = page_num - half_max_page
 # 根據(jù)展示的總頁碼算出頁面上展示的頁碼到哪兒結(jié)束
 page_end = page_num + half_max_page

 # 如果當(dāng)前頁減一半 比1還小, 不然頁面上會顯示負(fù)數(shù)的頁碼
 if page_start <= 1:
 page_start = 1
 page_end = max_page
 # 如果 當(dāng)前頁 加 一半 比總頁碼數(shù)還大, 不然頁面上會顯示比總頁碼還大的多余頁碼
 if page_end >= total_page:
 page_end = total_page
 page_start = total_page - max_page + 1

 # 從數(shù)據(jù)庫取值, 并按照起始數(shù)據(jù)到結(jié)束數(shù)據(jù)展示
 all_book = models.Book.objects.all()[data_start:data_end]


 # 自己拼接分頁的HTML代碼
 html_str_list = []

 # # 加上首頁
 html_str_list.append('<li><a href="/book/?page=1" rel="external nofollow" >首頁</a></li>')

 # 斷一下 如果是第一頁,就沒有上一頁
 if page_num <= 1:
 html_str_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">&laquo;</span></a></li>')
 else:
 # 不是第一頁,就加一個上一頁的標(biāo)簽
 html_str_list.append('<li><a href="/book/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">&laquo;</span></a></li>'.format(page_num - 1))

 for i in range(page_start, page_end + 1):
 # 如果是當(dāng)前頁就加一個active樣式類
 if i == page_num:
  tmp = '<li class="active"><a href="/book/?page={0}" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
 else:
  tmp = '<li><a href="/book/?page={0}" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)

 html_str_list.append(tmp)

 # 判斷,如果是最后一頁,就沒有下一頁
 if page_num >= total_page:
 html_str_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">&raquo;</span></a></li>')
 else:
 # 不是最后一頁, 就加一個下一頁標(biāo)簽
 html_str_list.append('<li><a href="/book/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">&raquo;</span></a></li>'.format(page_num + 1))

 # 加上尾頁
 html_str_list.append('<li><a href="/book/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a></li>'.format(total_page))

 page_html = "".join(html_str_list)
 return render(request, "book.html", {"all_book":all_book, "page_html":page_html})

book.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>書籍列表</title>
 <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<table class="table table-bordered">
 <thead>
 <tr>
  <th>序列號</th>
  <th>ID值</th>
  <th>書名</th>
  <th>時間</th>
 </tr>
 {% for book in all_book %}
 <tr>
  <td>{{ forloop.counter }}</td>
  <td>{{ book.id }}</td>
  <td>{{ book.name }}</td>
  <td>{{ book.date }}</td>
 </tr>
 {% endfor %}
 </thead>
</table>
<nav aria-label="Page navigation">
 <ul class="pagination">
 {{ page_html|safe }}
 </ul>
</nav>
</div>
</body>
</html>

封裝保存版

封裝保存版

class Page(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ù)
 :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轉(zhuǎn)換為int類型
  page_num = int(page_num)
 except Exception as e:
  # 當(dāng)輸入的頁碼不是正經(jīng)數(shù)字的時候 默認(rèn)返回第一頁的數(shù)據(jù)
  page_num = 1
 # 如果輸入的頁碼數(shù)超過了最大的頁碼數(shù),默認(rèn)返回最后一頁
 if page_num > total_page:
  page_num = total_page
 self.page_num = page_num

 # 定義兩個變量保存數(shù)據(jù)從哪兒取到哪兒
 self.data_start = (page_num - 1) * 10
 self.data_end = page_num * 10

 # 頁面上總共展示多少頁碼
 if total_page < self.max_page:
  self.max_page = total_page

 half_max_page = self.max_page // 2
 # 頁面上展示的頁碼從哪兒開始
 page_start = page_num - half_max_page
 # 頁面上展示的頁碼到哪兒結(jié)束
 page_end = page_num + half_max_page
 # 如果當(dāng)前頁減一半 比1還小, 不然頁面上會顯示負(fù)數(shù)的頁碼
 if page_start <= 1:
  page_start = 1
  page_end = self.max_page
 # 如果 當(dāng)前頁 加 一半 比總頁碼數(shù)還大, 不然頁面上會顯示比總頁碼還大的多余頁碼
 if page_end >= total_page:
  page_end = total_page
  page_start = 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_str_list = []
 # # 加上首頁
 html_str_list.append('<li><a href="{}?page=1">首頁</a></li>'.format(self.url_prefix))
 # 斷一下 如果是第一頁,就沒有上一頁
 if self.page_num <= 1:
  html_str_list.append('<li class="disabled"><a href="#"><span aria-hidden="true">&laquo;</span></a></li>')
 else:
  # 不是第一頁,就加一個上一頁的標(biāo)簽
  html_str_list.append('<li><a href="{}?page={}"><span aria-hidden="true">&laquo;</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}">{1}</a></li>'.format(self.url_prefix, i)
  else:
  tmp = '<li><a href="{0}?page={1}">{1}</a></li>'.format(self.url_prefix, i)

  html_str_list.append(tmp)

 # 判斷,如果是最后一頁,就沒有下一頁
 if self.page_num >= self.total_page:
  html_str_list.append('<li class="disabled"><a href="#"><span aria-hidden="true">&raquo;</span></a></li>')
 else:
  # 不是最后一頁, 就加一個下一頁標(biāo)簽
  html_str_list.append('<li><a href="{}?page={}"><span aria-hidden="true">&raquo;</span></a></li>'.format(self.url_prefix, self.page_num + 1))

 # 加上尾頁
 html_str_list.append('<li><a href="{}?page={}">尾頁</a></li>'.format(self.url_prefix, self.total_page))

 page_html = "".join(html_str_list)
 return page_html

封裝版使用指南

def publisher(request):
 page_num = request.GET.get("page")
 total_count = models.Publisher.objects.all().count()
 # 調(diào)用封裝的Page類,傳入相應(yīng)的參數(shù)
 page_obj = Page(page_num, total_count, url_prefix="/publisher/", per_page=10, max_page=11)
 all_publisher = models.Publisher.objects.all()[page_obj.start:page_obj.end]
 page_html = page_obj.page_html()
 return render(request, "publisher.html", {"publisher": all_publisher, "page_html": page_html})

封裝版對應(yīng)的HTML參考

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>圖書列表</title>
 <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
 <table class="table table-bordered">
 <thead>
 <tr>
  <td>序列號</td>
  <td>ID值</td>
  <td>出版社</td>
  <td>時間</td>
 </tr>
 </thead>
 <tbody>
 {% for pub in publisher %}
  <tr>
  <th>{{ forloop.counter }}</th>
  <th>{{ pub.id }}</th>
  <th>{{ pub.name }}</th>
  <th>{{ pub.date }}</th>
  </tr>
 {% endfor %}
 </tbody>
 </table>
 <nav aria-label="Page navigation">
 <ul class="pagination">
  {{ page_html|safe }}
 </ul>
 </nav>
</div>
</body>
</html>

效果圖如下:

Django自定義分頁的示例

感謝各位的閱讀!關(guān)于“Django自定義分頁的示例”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向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