欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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
  # 頁面上展示的頁碼的結(jié)束頁
  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>

運行結(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) 
  # 定義兩個變量保存數(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 
  # 如果當前頁減一半比 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>

運行結(jié)果:

后續(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
  # 頁面上展示的頁碼的結(jié)束頁
  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
  # 頁面上展示的頁碼的結(jié)束頁
  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})

運行結(jié)果:

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

相關文章

  • Python對wav文件的重采樣實例

    Python對wav文件的重采樣實例

    今天小編就為大家分享一篇Python對wav文件的重采樣實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python使用python-pptx刪除ppt某頁實例

    python使用python-pptx刪除ppt某頁實例

    今天小編就為大家分享一篇python使用python-pptx刪除ppt某頁實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法匯總

    Python Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法匯總

    這篇文章主要介紹了Python中的Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法,創(chuàng)建Dataframe主要是使用pandas中的DataFrame函數(shù),其核心就是第一個參數(shù):data,傳入原始數(shù)據(jù),因此我們可以據(jù)此給出六種創(chuàng)建Dataframe的方法,需要的朋友可以參考下
    2023-05-05
  • python爬蟲Mitmproxy安裝使用學習筆記

    python爬蟲Mitmproxy安裝使用學習筆記

    這篇文章主要介紹了python爬蟲Mitmproxy學習筆記分享,有需要的朋友可以收藏學習下,希望可以對你有所幫助,大家一起共同學習,共同進步
    2021-09-09
  • DjangoWeb使用Datatable進行后端分頁的實現(xiàn)

    DjangoWeb使用Datatable進行后端分頁的實現(xiàn)

    這篇文章主要介紹了DjangoWeb使用Datatable進行后端分頁的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細使用方法與實例

    python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細使用方法與實例

    這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細使用方法與實例,需要的朋友可以參考下
    2020-02-02
  • Python 如何提高元組的可讀性

    Python 如何提高元組的可讀性

    這篇文章主要介紹了Python 如何提高元組的可讀性,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • python 制作python包,封裝成可用模塊教程

    python 制作python包,封裝成可用模塊教程

    這篇文章主要介紹了python 制作python包,封裝成可用模塊教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python中Collections模塊的Counter容器類使用教程

    Python中Collections模塊的Counter容器類使用教程

    Counter是Python標準庫提供的一個非常有用的容器,可以用來對序列中出現(xiàn)的各個元素進行計數(shù),下面就來一起看一下Python中Collections模塊的Counter容器類使用教程
    2016-05-05
  • Python對excel文檔的操作方法詳解

    Python對excel文檔的操作方法詳解

    這篇文章主要介紹了Python對excel文檔的操作方法,結(jié)合實例形式分析了Python基于xlrd、xlwd庫針對Excel文件的讀寫、sheet表創(chuàng)建、獲取、遍歷等相關操作技巧,需要的朋友可以參考下
    2018-12-12

最新評論