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

Django框架實現(xiàn)的分頁demo示例

 更新時間:2019年05月25日 09:47:32   作者:SpecYue  
這篇文章主要介紹了Django框架實現(xiàn)的分頁demo,結(jié)合實例形式分析了Django框架分頁的步驟、原理、相關(guān)操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了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">&laquo;</span>
   </a>
  </li>
   {{ page_html|safe }}
  <li>
   <a href="#" rel="external nofollow" rel="external nofollow" aria-label="Next">
    <span aria-hidden="true">&raquo;</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極簡入門教程

    Pytorch學(xué)習(xí)筆記DCGAN極簡入門教程

    網(wǎng)上GAN的教程太多了,這邊也談一下自己的理解,本文給大家介紹一下GAN的兩部分組成,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • Python異步爬取知乎熱榜實例分享

    Python異步爬取知乎熱榜實例分享

    這篇文章主要介紹了Python異步爬取知乎熱榜實例分享,文章圍繞Python異步爬取是我相關(guān)資料展開對知乎熱榜爬取的相關(guān)內(nèi)容,需要的小伙伴卡哇伊參考一下
    2022-04-04
  • 1行Python代碼實現(xiàn)去除圖片水印詳解

    1行Python代碼實現(xiàn)去除圖片水印詳解

    這篇文章主要為大家介紹了1行Python代碼實現(xiàn)去除圖片水印詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • python的help函數(shù)如何使用

    python的help函數(shù)如何使用

    在本篇文章里小編給大家整理的是關(guān)于python的help函數(shù)的相關(guān)用法和知識點總結(jié),需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • python批量下載抖音視頻

    python批量下載抖音視頻

    這篇文章主要為大家詳細介紹了python批量下載抖音視頻,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Django 開發(fā)環(huán)境配置過程詳解

    Django 開發(fā)環(huán)境配置過程詳解

    這篇文章主要介紹了Django 開發(fā)環(huán)境配置過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Python自動連接ssh的方法

    Python自動連接ssh的方法

    這篇文章主要介紹了Python自動連接ssh的方法,實例分析了基于Python實現(xiàn)連接ssh的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • python中如何實現(xiàn)將數(shù)據(jù)分成訓(xùn)練集與測試集的方法

    python中如何實現(xiàn)將數(shù)據(jù)分成訓(xùn)練集與測試集的方法

    這篇文章主要介紹了python中如何實現(xiàn)將數(shù)據(jù)分成訓(xùn)練集與測試集的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Python機器學(xué)習(xí)庫scikit-learn安裝與基本使用教程

    Python機器學(xué)習(xí)庫scikit-learn安裝與基本使用教程

    這篇文章主要介紹了Python機器學(xué)習(xí)庫scikit-learn安裝與基本使用,較為詳細的介紹了機器學(xué)習(xí)庫scikit-learn的功能、原理、基本安裝與簡單使用方法,需要的朋友可以參考下
    2018-06-06
  • 在Django的模型中執(zhí)行原始SQL查詢的方法

    在Django的模型中執(zhí)行原始SQL查詢的方法

    這篇文章主要介紹了在Django的模型中執(zhí)行原始SQL查詢的方法,Django是最具人氣的Python web開發(fā)框架,需要的朋友可以參考下
    2015-07-07

最新評論