亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Django如何自定義分頁(yè)

 更新時(shí)間:2018年09月25日 08:50:38   作者:別來(lái)無(wú)恙-  
這篇文章主要為大家詳細(xì)介紹了Django自定義分頁(yè)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Django自定義分頁(yè)的具體代碼,供大家參考,具體內(nèi)容如下

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

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

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

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

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

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

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

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

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

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

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


 # 自己拼接分頁(yè)的HTML代碼
 html_str_list = []

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

 # 斷一下 如果是第一頁(yè),就沒(méi)有上一頁(yè)
 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:
 # 不是第一頁(yè),就加一個(gè)上一頁(yè)的標(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)前頁(yè)就加一個(gè)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)

 # 判斷,如果是最后一頁(yè),就沒(méi)有下一頁(yè)
 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:
 # 不是最后一頁(yè), 就加一個(gè)下一頁(yè)標(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))

 # 加上尾頁(yè)
 html_str_list.append('<li><a href="/book/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁(yè)</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>序列號(hào)</th>
  <th>ID值</th>
  <th>書名</th>
  <th>時(shí)間</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)前頁(yè)碼數(shù)
 :param total_count: 數(shù)據(jù)總數(shù)
 :param url_prefix: a標(biāo)簽href的前綴
 :param per_page: 每頁(yè)顯示多少條數(shù)據(jù)
 :param max_page: 頁(yè)面上最多顯示幾個(gè)頁(yè)碼
 """
 self.url_prefix = url_prefix
 self.max_page = max_page
 # 總共需要多少頁(yè)碼來(lái)展示
 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)輸入的頁(yè)碼不是正經(jīng)數(shù)字的時(shí)候 默認(rèn)返回第一頁(yè)的數(shù)據(jù)
  page_num = 1
 # 如果輸入的頁(yè)碼數(shù)超過(guò)了最大的頁(yè)碼數(shù),默認(rèn)返回最后一頁(yè)
 if page_num > total_page:
  page_num = total_page
 self.page_num = page_num

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

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

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

 # 判斷,如果是最后一頁(yè),就沒(méi)有下一頁(yè)
 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:
  # 不是最后一頁(yè), 就加一個(gè)下一頁(yè)標(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))

 # 加上尾頁(yè)
 html_str_list.append('<li><a href="{}?page={}">尾頁(yè)</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})

封裝版對(duì)應(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>序列號(hào)</td>
  <td>ID值</td>
  <td>出版社</td>
  <td>時(shí)間</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>

效果圖如下:

 以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中IO多路復(fù)用模塊selector的用法詳解

    Python中IO多路復(fù)用模塊selector的用法詳解

    selector?是一個(gè)實(shí)現(xiàn)了IO復(fù)用模型的python包,實(shí)現(xiàn)了IO多路復(fù)用模型的?select、poll?和?epoll?等函數(shù),下面就跟隨小編一起來(lái)學(xué)習(xí)一下它的具體使用吧
    2024-02-02
  • Python 實(shí)現(xiàn)PS濾鏡中的徑向模糊特效

    Python 實(shí)現(xiàn)PS濾鏡中的徑向模糊特效

    這篇文章主要介紹了Python 實(shí)現(xiàn) PS 濾鏡中的徑向模糊特效,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下
    2020-12-12
  • Python安裝第三方庫(kù)的3種方法

    Python安裝第三方庫(kù)的3種方法

    這篇文章主要介紹了Python安裝第三方庫(kù)的3種方法,本文講解了通過(guò)setuptools來(lái)安裝python模塊、通過(guò)pip來(lái)安裝python模塊、直接從網(wǎng)上下載下可執(zhí)行文件來(lái)安裝三種方法,需要的朋友可以參考下
    2015-06-06
  • Pycharm安裝第三方庫(kù)的超詳細(xì)步驟

    Pycharm安裝第三方庫(kù)的超詳細(xì)步驟

    使用python時(shí),為了提高效率,安裝添加第三方庫(kù)是必不可少的,下面這篇文章主要給大家介紹了關(guān)于Pycharm安裝第三方庫(kù)的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • 150行python代碼實(shí)現(xiàn)貪吃蛇游戲

    150行python代碼實(shí)現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細(xì)介紹了150行python代碼實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 學(xué)好python基本數(shù)據(jù)類型

    學(xué)好python基本數(shù)據(jù)類型

    這篇文章主要介紹了學(xué)好python基本數(shù)據(jù)類型,學(xué)習(xí)python基本數(shù)據(jù)類型我們需要了解基本數(shù)據(jù)類型有數(shù)字int、布爾值bool、字符串str、列表list、元組tuple、字典dict等,其中包括他們的基本用法和其常用的方法,下面來(lái)看看文章的具體介紹吧
    2021-12-12
  • python+openCV調(diào)用攝像頭拍攝和處理圖片的實(shí)現(xiàn)

    python+openCV調(diào)用攝像頭拍攝和處理圖片的實(shí)現(xiàn)

    這篇文章主要介紹了python+openCV調(diào)用攝像頭拍攝和處理圖片的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python字符串詳細(xì)介紹

    Python字符串詳細(xì)介紹

    這篇文章主要介紹了Python字符串詳解,本文講解了字符串相關(guān)知識(shí)、字符串的一些特性、原始字符串、unicode字符串、字符串的常用操作方法、內(nèi)建函數(shù)列表等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • Python 實(shí)現(xiàn)局域網(wǎng)遠(yuǎn)程屏幕截圖案例

    Python 實(shí)現(xiàn)局域網(wǎng)遠(yuǎn)程屏幕截圖案例

    這篇文章主要介紹了Python 實(shí)現(xiàn)局域網(wǎng)遠(yuǎn)程屏幕截圖案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • Python類如何定義私有變量

    Python類如何定義私有變量

    這篇文章主要介紹了Python類如何定義私有變量,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論