最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Django 添加首頁尾頁上一頁下一頁代碼實例

 更新時間:2019年08月21日 11:19:18   作者:Sch01aR#  
這篇文章主要介紹了Python Django 添加首頁尾頁上一頁下一頁代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

添加首頁和尾頁:

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))
  page_num = int(page_num) 
  # 定義兩個變量保存數(shù)據(jù)從哪兒取到哪兒
  data_start = (page_num - 1) * 10
  data_end = page_num * 10 
  # 書籍總數(shù)
  total_count = models.Book.objects.all().count() 
  # 每一頁顯示多少條數(shù)據(jù)
  per_page = 10
  # 總共需要多少頁碼來顯示
  total_page, m = divmod(total_count, per_page) 
  # 頁面上最多展示的頁碼
  max_page = 11
  half_max_page = max_page // 2
 
  # 頁面上展示的頁碼的開始頁
  page_start = page_num - half_max_page
  # 頁面上展示的頁碼的結束頁
  page_end = page_num + half_max_page 
  # 如果當前頁減一半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 如果當前頁加一半比總頁碼還大
  if page_end > total_page:
    page_end = total_page
    page_start = total_page - max_page + 1 
  # 如果還有數(shù)據(jù)
  if m:
    total_page += 1 
  all_book = models.Book.objects.all()[data_start:data_end] 
  # 拼接 html 的分頁代碼
  html_list = [] 
  # 添加首頁按鈕
  html_list.append('<li><a href="/books/?page=1" rel="external nofollow" >首頁</a></li>')
  # 展示的頁碼
  for i in range(page_start, page_end + 1):
    tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
    html_list.append(tmp) 
  # 添加尾頁按鈕
  html_list.append('<li><a href="/books/?page={}" rel="external nofollow" >尾頁</a></li>'.format(total_page)) 
  page_html = "".join(html_list) # 拼接 html 的分頁代碼 
  return render(request, "book_list.html", {"books": all_book, "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" 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>

運行結果:

添加上一頁、下一頁:

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))
  page_num = int(page_num) 
  # 定義兩個變量保存數(shù)據(jù)從哪兒取到哪兒
  data_start = (page_num - 1) * 10
  data_end = page_num * 10 
  # 書籍總數(shù)
  total_count = models.Book.objects.all().count() 
  # 每一頁顯示多少條數(shù)據(jù)
  per_page = 10 
  # 總共需要多少頁碼來顯示
  total_page, m = divmod(total_count, per_page) 
  # 頁面上最多展示的頁碼
  max_page = 11
  half_max_page = max_page // 2 
  # 頁面上展示的頁碼的開始頁
  page_start = page_num - half_max_page
  # 頁面上展示的頁碼的結束頁
  page_end = page_num + half_max_page 
  # 如果當前頁減一半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 如果當前頁加一半比總頁碼還大
  if page_end > total_page:
    page_end = total_page
    page_start = total_page - max_page + 1 
  # 如果還有數(shù)據(jù)
  if m:
    total_page += 1 
  all_book = models.Book.objects.all()[data_start:data_end] 
  # 拼接 html 的分頁代碼
  html_list = [] 
  # 添加首頁按鈕
  html_list.append('<li><a href="/book_list/?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a></li>') 
  # 如果是第一頁,就沒有上一頁
  if page_num <= 1:
    html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))
  else:
    # 加一個上一頁的標簽
    html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1)) 
  # 展示的頁碼
  for i in range(page_start, page_end + 1):
    # 給當前頁添加 active
    if i == page_num:
      tmp = '<li class="active"><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
    else:
      tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
    html_list.append(tmp) 
  # 如果是最后一頁,就沒有下一頁
  if page_num >= total_page:
    html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>')
  else:
    html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>'.format(page_num+1)) 
  # 添加尾頁按鈕
  html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a></li>'.format(total_page))
 
  page_html = "".join(html_list) # 拼接 html 的分頁代碼
  return render(request, "book_list.html", {"books": all_book, "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" 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>

運行結果:

后續(xù)改進:

處理用戶傳給 url 的 page 參數(shù)異常的值的情況

例如:

訪問,http://127.0.0.1:8888/book_list/?page=a

訪問,http://127.0.0.1:8888/book_list/?page=-1

都會出錯

改進:

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)) # page_num 為 str 類型
  # 書籍總數(shù)
  total_count = models.Book.objects.all().count() 
  # 每一頁顯示多少條數(shù)據(jù)
  per_page = 10 
  # 總共需要多少頁碼來顯示
  total_page, m = divmod(total_count, per_page) 
  # 如果還有數(shù)據(jù)
  if m:
    total_page += 1
  try:
    page_num = int(page_num)
    # 如果輸入的頁碼數(shù)超過了最大的頁碼數(shù),默認返回最后一頁
    if page_num > total_page:
      page_num = total_page
    # 如果輸入的頁碼數(shù)小于 1,則返回第一頁
    if page_num < 1:
      page_num = 1
  except Exception as e:
    # 當輸入的頁碼不是正經(jīng)數(shù)字的時候 默認返回第一頁的數(shù)據(jù)
    page_num = 1
  # 定義兩個變量保存數(shù)據(jù)從哪兒取到哪兒
  data_start = (page_num - 1) * 10
  data_end = page_num * 10 
  # 頁面上最多展示的頁碼
  max_page = 11
  half_max_page = max_page // 2
  # 頁面上展示的頁碼的開始頁
  page_start = page_num - half_max_page
  # 頁面上展示的頁碼的結束頁
  page_end = page_num + half_max_page 
  # 如果當前頁減一半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 如果當前頁加一半比總頁碼還大
  if page_end > total_page:
    page_end = total_page
    page_start = total_page - max_page + 1 
  all_book = models.Book.objects.all()[data_start:data_end] 
  # 拼接 html 的分頁代碼
  html_list = [] 
  # 添加首頁按鈕
  html_list.append('<li><a href="/book_list/?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a></li>') 
  # 如果是第一頁,就沒有上一頁
  if page_num <= 1:
    html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))
  else:
    # 加一個上一頁的標簽
    html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))
   # 展示的頁碼
  for i in range(page_start, page_end + 1):
    # 給當前頁添加 active
    if i == page_num:
      tmp = '<li class="active"><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
    else:
      tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
    html_list.append(tmp) 
  # 如果是最后一頁,就沒有下一頁
  if page_num >= total_page:
    html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>')
  else:
    html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>'.format(page_num+1)) 
  # 添加尾頁按鈕
  html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a></li>'.format(total_page))
 
  page_html = "".join(html_list) # 拼接 html 的分頁代碼
  return render(request, "book_list.html", {"books": all_book, "page_html": page_html})

如果數(shù)據(jù)庫中的數(shù)據(jù)數(shù)少于 max_page,則會顯示負數(shù)的頁數(shù)

例如數(shù)據(jù)庫中只有 21 條數(shù)據(jù):

改進:

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)) # page_num 為 str 類型 
  # 書籍總數(shù)
  total_count = models.Book.objects.all().count() 
  # 每一頁顯示多少條數(shù)據(jù)
  per_page = 10 
  # 總共需要多少頁碼來顯示
  total_page, m = divmod(total_count, per_page) 
  # 如果還有數(shù)據(jù)
  if m:
    total_page += 1
  try:
    page_num = int(page_num)
    # 如果輸入的頁碼數(shù)超過了最大的頁碼數(shù),默認返回最后一頁
    if page_num > total_page:
      page_num = total_page
    # 如果輸入的頁碼數(shù)小于 1,則返回第一頁
    if page_num < 1:
      page_num = 1
  except Exception as e:
    # 當輸入的頁碼不是正經(jīng)數(shù)字的時候 默認返回第一頁的數(shù)據(jù)
    page_num = 1 
  # 定義兩個變量保存數(shù)據(jù)從哪兒取到哪兒
  data_start = (page_num - 1) * 10
  data_end = page_num * 10 
  # 頁面上最多展示的頁碼
  max_page = 11
  # 如果總頁碼數(shù)小于頁面上最多展示的頁碼
  if total_page < max_page:
    max_page = total_page
  half_max_page = max_page // 2 
  # 頁面上展示的頁碼的開始頁
  page_start = page_num - half_max_page
  # 頁面上展示的頁碼的結束頁
  page_end = page_num + half_max_page 
  # 如果當前頁減一半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 如果當前頁加一半比總頁碼還大
  if page_end > total_page:
    page_end = total_page
    page_start = total_page - max_page + 1 
  all_book = models.Book.objects.all()[data_start:data_end] 
  # 拼接 html 的分頁代碼
  html_list = [] 
  # 添加首頁按鈕
  html_list.append('<li><a href="/book_list/?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a></li>') 
  # 如果是第一頁,就沒有上一頁
  if page_num <= 1:
    html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))
  else:
    # 加一個上一頁的標簽
    html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))
  # 展示的頁碼
  for i in range(page_start, page_end + 1):
    # 給當前頁添加 active
    if i == page_num:
      tmp = '<li class="active"><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
    else:
      tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)
    html_list.append(tmp) 
  # 如果是最后一頁,就沒有下一頁
  if page_num >= total_page:
    html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>')
  else:
    html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>'.format(page_num+1)) 
  # 添加尾頁按鈕
  html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a></li>'.format(total_page)) 
  page_html = "".join(html_list) # 拼接 html 的分頁代碼
  return render(request, "book_list.html", {"books": all_book, "page_html": page_html})

運行結果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Python配置文件yaml的用法詳解

    Python配置文件yaml的用法詳解

    YAML是一種直觀的能夠被電腦識別的的數(shù)據(jù)序列化格式,容易被人類閱讀,并且容易和腳本語言交互。本文將詳細介紹一下Python中yaml文件的用法,需要的可以參考一下
    2022-03-03
  • OpenAI的Whisper模型進行語音識別使用詳解

    OpenAI的Whisper模型進行語音識別使用詳解

    這篇文章主要介紹了OpenAI的Whisper模型進行語音識別使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • pytorch 獲取層權重,對特定層注入hook, 提取中間層輸出的方法

    pytorch 獲取層權重,對特定層注入hook, 提取中間層輸出的方法

    今天小編就為大家分享一篇pytorch 獲取層權重,對特定層注入hook, 提取中間層輸出的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 利用PyInstaller將python程序.py轉為.exe的方法詳解

    利用PyInstaller將python程序.py轉為.exe的方法詳解

    這篇文章主要給大家介紹了利用PyInstaller將python程序.py轉為.exe的方法,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • python?ocr簡單示例之識別驗證碼

    python?ocr簡單示例之識別驗證碼

    OCR(Optical character recognition,光學字符識別)是一種將圖像中的手寫字或者印刷文本轉換為機器編碼文本的技術,下面這篇文章主要給大家介紹了關于python?ocr簡單示例之識別驗證碼的相關資料,需要的朋友可以參考下
    2023-01-01
  • python入門學習關于for else的特殊特性講解

    python入門學習關于for else的特殊特性講解

    本文將介紹 Python 中的" for-else"特性,并通過簡單的示例說明如何正確使用它,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • 一行Python代碼過濾標點符號等特殊字符

    一行Python代碼過濾標點符號等特殊字符

    這篇文章主要介紹了一行Python代碼過濾標點符號等特殊字符的相關知識,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • 解決Mac下使用python的坑

    解決Mac下使用python的坑

    今天小編就為大家分享一篇解決Mac下使用python的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • PyCharm設置中文(漢化與解除漢化)的方法

    PyCharm設置中文(漢化與解除漢化)的方法

    這篇文章介紹了PyCharm設置中文(漢化與解除漢化)的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • Python爬蟲代理IP池實現(xiàn)方法

    Python爬蟲代理IP池實現(xiàn)方法

    在公司做分布式深網(wǎng)爬蟲,搭建了一套穩(wěn)定的代理池服務,為上千個爬蟲提供有效的代理,保證各個爬蟲拿到的都是對應網(wǎng)站有效的代理IP,從而保證爬蟲快速穩(wěn)定的運行,所以就想利用一些免費的資源搞一個簡單的代理池服務。
    2017-01-01

最新評論

金塔县| 广宗县| 喜德县| 水富县| 新龙县| 武乡县| 科技| 尤溪县| 洪雅县| 年辖:市辖区| 信丰县| 修水县| 开化县| 中阳县| 吐鲁番市| 文昌市| 宝山区| 开江县| 随州市| 葫芦岛市| 汶上县| 修文县| 济阳县| 辽宁省| 大城县| 永修县| 昌乐县| 张家界市| 天柱县| 普兰县| 望江县| 祁阳县| 隆尧县| 伊春市| 信宜市| 甘洛县| 宁南县| 伊吾县| 高淳县| 灌云县| 襄汾县|