Python Django 添加首页尾页上一页下一页代码实例


Posted in Python onAugust 21, 2019

添加首页和尾页:

views.py:

from django.shortcuts import render
from app01 import models
def book_list(request):
  # 从 URL 中取参数
  page_num = request.GET.get("page")
  print(page_num, type(page_num))
  page_num = int(page_num) 
  # 定义两个变量保存数据从哪儿取到哪儿
  data_start = (page_num - 1) * 10
  data_end = page_num * 10 
  # 书籍总数
  total_count = models.Book.objects.all().count() 
  # 每一页显示多少条数据
  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 
  # 如果还有数据
  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>

运行结果:

Python Django 添加首页尾页上一页下一页代码实例

添加上一页、下一页:

views.py:

from django.shortcuts import render
from app01 import models 
def book_list(request):
  # 从 URL 中取参数
  page_num = request.GET.get("page")
  print(page_num, type(page_num))
  page_num = int(page_num) 
  # 定义两个变量保存数据从哪儿取到哪儿
  data_start = (page_num - 1) * 10
  data_end = page_num * 10 
  # 书籍总数
  total_count = models.Book.objects.all().count() 
  # 每一页显示多少条数据
  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 
  # 如果还有数据
  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>

运行结果:

Python Django 添加首页尾页上一页下一页代码实例

后续改进:

处理用户传给 url 的 page 参数异常的值的情况

例如:

访问,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 中取参数
  page_num = request.GET.get("page")
  print(page_num, type(page_num)) # page_num 为 str 类型
  # 书籍总数
  total_count = models.Book.objects.all().count() 
  # 每一页显示多少条数据
  per_page = 10 
  # 总共需要多少页码来显示
  total_page, m = divmod(total_count, per_page) 
  # 如果还有数据
  if m:
    total_page += 1
  try:
    page_num = int(page_num)
    # 如果输入的页码数超过了最大的页码数,默认返回最后一页
    if page_num > total_page:
      page_num = total_page
    # 如果输入的页码数小于 1,则返回第一页
    if page_num < 1:
      page_num = 1
  except Exception as e:
    # 当输入的页码不是正经数字的时候 默认返回第一页的数据
    page_num = 1
  # 定义两个变量保存数据从哪儿取到哪儿
  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})

如果数据库中的数据数少于 max_page,则会显示负数的页数

例如数据库中只有 21 条数据:

Python Django 添加首页尾页上一页下一页代码实例

改进:

from django.shortcuts import render
from app01 import models
def book_list(request):
  # 从 URL 中取参数
  page_num = request.GET.get("page")
  print(page_num, type(page_num)) # page_num 为 str 类型 
  # 书籍总数
  total_count = models.Book.objects.all().count() 
  # 每一页显示多少条数据
  per_page = 10 
  # 总共需要多少页码来显示
  total_page, m = divmod(total_count, per_page) 
  # 如果还有数据
  if m:
    total_page += 1
  try:
    page_num = int(page_num)
    # 如果输入的页码数超过了最大的页码数,默认返回最后一页
    if page_num > total_page:
      page_num = total_page
    # 如果输入的页码数小于 1,则返回第一页
    if page_num < 1:
      page_num = 1
  except Exception as e:
    # 当输入的页码不是正经数字的时候 默认返回第一页的数据
    page_num = 1 
  # 定义两个变量保存数据从哪儿取到哪儿
  data_start = (page_num - 1) * 10
  data_end = page_num * 10 
  # 页面上最多展示的页码
  max_page = 11
  # 如果总页码数小于页面上最多展示的页码
  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})

运行结果:

Python Django 添加首页尾页上一页下一页代码实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
测试、预发布后用python检测网页是否有日常链接
Jun 03 Python
python 的列表遍历删除实现代码
Apr 12 Python
CentOS 6.5中安装Python 3.6.2的方法步骤
Dec 03 Python
Python通过调用mysql存储过程实现更新数据功能示例
Apr 03 Python
用pandas中的DataFrame时选取行或列的方法
Jul 11 Python
十个Python练手的实战项目,学会这些Python就基本没问题了(推荐)
Apr 26 Python
Python+Selenium使用Page Object实现页面自动化测试
Jul 14 Python
python字符串分割及字符串的一些常规方法
Jul 24 Python
Pyorch之numpy与torch之间相互转换方式
Dec 31 Python
win10安装python3.6的常见问题
Jul 01 Python
django如何自定义manage.py管理命令
Apr 27 Python
pytorch分类模型绘制混淆矩阵以及可视化详解
Apr 07 Python
Python Django 简单分页的实现代码解析
Aug 21 #Python
Django项目之Elasticsearch搜索引擎的实例
Aug 21 #Python
python爬虫豆瓣网的模拟登录实现
Aug 21 #Python
Python Django 页面上展示固定的页码数实现代码
Aug 21 #Python
详解Python利用random生成一个列表内的随机数
Aug 21 #Python
Python Django 封装分页成通用的模块详解
Aug 21 #Python
Django之编辑时根据条件跳转回原页面的方法
Aug 21 #Python
You might like
php 来访国内外IP判断代码并实现页面跳转
2009/12/18 PHP
discuz免激活同步登入代码修改方法(discuz同步登录)
2013/12/24 PHP
WordPress主题制作中自定义头部的相关PHP函数解析
2016/01/08 PHP
PHP实现二维数组按照指定的字段进行排序算法示例
2019/04/23 PHP
YII2框架使用控制台命令的方法分析
2020/03/18 PHP
给网站上的广告“加速”显示的方法
2007/04/08 Javascript
jQuery EasyUI API 中文文档 - Dialog对话框
2011/11/15 Javascript
什么是json和jsonp,jQuery json实例详详细说明
2012/12/11 Javascript
js获取键盘按键响应事件(兼容各浏览器)
2013/05/16 Javascript
用html5 js实现点击一个按钮达到浏览器全屏效果
2014/05/28 Javascript
IE9+已经不对document.createElement向下兼容的解决方法
2015/09/14 Javascript
Nodejs Express4.x开发框架随手笔记
2015/11/23 NodeJs
jQuery原理系列-常用Dom操作详解
2016/06/07 Javascript
vue之浏览器存储方法封装实例
2018/03/15 Javascript
Vue Object 的变化侦测实现代码
2020/04/15 Javascript
查看TensorFlow checkpoint文件中的变量名和对应值方法
2018/06/14 Python
python 读取目录下csv文件并绘制曲线v111的方法
2018/07/06 Python
Python计算开方、立方、圆周率,精确到小数点后任意位的方法
2018/07/17 Python
python调用摄像头拍摄数据集
2019/06/01 Python
Python 使用元类type创建类对象常见应用详解
2019/10/17 Python
pygame实现五子棋游戏
2019/10/29 Python
Python使用psutil获取进程信息的例子
2019/12/17 Python
pymysql模块使用简介与示例
2020/11/17 Python
英国最大的自有市场,比亚马逊便宜:Flubit
2019/03/19 全球购物
好学生评语大全
2014/05/05 职场文书
公安四风对照检查材料思想汇报
2014/10/11 职场文书
工作疏忽检讨书500字
2014/10/26 职场文书
2014年司机工作总结
2014/11/21 职场文书
生产车间管理制度
2015/08/04 职场文书
一文搞懂如何实现Go 超时控制
2021/03/30 Python
Python基础之Socket通信原理
2021/04/22 Python
Python+Appium实现自动抢微信红包
2021/05/21 Python
用python修改excel表某一列内容的操作方法
2021/06/11 Python
Nginx 路由转发和反向代理location配置实现
2021/11/11 Servers
Python帮你解决手机qq微信内存占用太多问题
2022/02/15 Python
如何用六步教会你使用python爬虫爬取数据
2022/04/06 Python