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

Python Django 添加首頁尾頁上一頁下一頁代碼實(shí)例

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

添加首頁和尾頁:

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) 
  # 定義兩個(gè)變量保存數(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
  # 頁面上展示的頁碼的結(jié)束頁
  page_end = page_num + half_max_page 
  # 如果當(dāng)前頁減一半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 如果當(dāng)前頁加一半比總頁碼還大
  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>序號(hào)</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>

運(yùn)行結(jié)果:

添加上一頁、下一頁:

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) 
  # 定義兩個(gè)變量保存數(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
  # 頁面上展示的頁碼的結(jié)束頁
  page_end = page_num + half_max_page 
  # 如果當(dāng)前頁減一半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 如果當(dāng)前頁加一半比總頁碼還大
  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:
    # 加一個(gè)上一頁的標(biāo)簽
    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):
    # 給當(dāng)前頁添加 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>序號(hào)</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>

運(yùn)行結(jié)果:

后續(xù)改進(jìn):

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

例如:

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

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

都會(huì)出錯(cuò)

改進(jìn):

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ù),默認(rèn)返回最后一頁
    if page_num > total_page:
      page_num = total_page
    # 如果輸入的頁碼數(shù)小于 1,則返回第一頁
    if page_num < 1:
      page_num = 1
  except Exception as e:
    # 當(dāng)輸入的頁碼不是正經(jīng)數(shù)字的時(shí)候 默認(rèn)返回第一頁的數(shù)據(jù)
    page_num = 1
  # 定義兩個(gè)變量保存數(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
  # 頁面上展示的頁碼的結(jié)束頁
  page_end = page_num + half_max_page 
  # 如果當(dāng)前頁減一半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 如果當(dāng)前頁加一半比總頁碼還大
  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:
    # 加一個(gè)上一頁的標(biāo)簽
    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):
    # 給當(dāng)前頁添加 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,則會(huì)顯示負(fù)數(shù)的頁數(shù)

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

改進(jìn):

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ù),默認(rèn)返回最后一頁
    if page_num > total_page:
      page_num = total_page
    # 如果輸入的頁碼數(shù)小于 1,則返回第一頁
    if page_num < 1:
      page_num = 1
  except Exception as e:
    # 當(dāng)輸入的頁碼不是正經(jīng)數(shù)字的時(shí)候 默認(rèn)返回第一頁的數(shù)據(jù)
    page_num = 1 
  # 定義兩個(gè)變量保存數(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
  # 頁面上展示的頁碼的結(jié)束頁
  page_end = page_num + half_max_page 
  # 如果當(dāng)前頁減一半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 如果當(dāng)前頁加一半比總頁碼還大
  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:
    # 加一個(gè)上一頁的標(biāo)簽
    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):
    # 給當(dāng)前頁添加 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})

運(yùn)行結(jié)果:

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

相關(guān)文章

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

    Python配置文件yaml的用法詳解

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

    OpenAI的Whisper模型進(jìn)行語音識(shí)別使用詳解

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

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

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

    利用PyInstaller將python程序.py轉(zhuǎn)為.exe的方法詳解

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

    python?ocr簡單示例之識(shí)別驗(yàn)證碼

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

    python入門學(xué)習(xí)關(guān)于for else的特殊特性講解

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

    一行Python代碼過濾標(biāo)點(diǎn)符號(hào)等特殊字符

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

    解決Mac下使用python的坑

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

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

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

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

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

最新評論

庄浪县| 汾阳市| 荃湾区| 霍城县| 叙永县| 延长县| 郑州市| 永州市| 馆陶县| 晋江市| 揭西县| 龙里县| 葫芦岛市| 石阡县| 图木舒克市| 桐梓县| 宜川县| 宾阳县| 泌阳县| 屏南县| 保定市| 融水| 礼泉县| 和静县| 龙川县| 桦甸市| 神池县| 唐海县| 隆林| 宜昌市| 循化| 台安县| 德钦县| 祁阳县| 喀什市| 新巴尔虎左旗| 邹城市| 土默特右旗| 城市| 彭州市| 西宁市|