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 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-05DjangoWeb使用Datatable進行后端分頁的實現(xiàn)
這篇文章主要介紹了DjangoWeb使用Datatable進行后端分頁的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細使用方法與實例,需要的朋友可以參考下2020-02-02Python中Collections模塊的Counter容器類使用教程
Counter是Python標準庫提供的一個非常有用的容器,可以用來對序列中出現(xiàn)的各個元素進行計數(shù),下面就來一起看一下Python中Collections模塊的Counter容器類使用教程2016-05-05