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写xml文件的操作实例
Oct 05 Python
Python入门篇之字典
Oct 17 Python
Python 正则表达式(转义问题)
Dec 15 Python
Python中用PIL库批量给图片加上序号的教程
May 06 Python
Python语言实现机器学习的K-近邻算法
Jun 11 Python
Django migrations 默认目录修改的方法教程
Sep 28 Python
对Python中plt的画图函数详解
Nov 07 Python
解决python super()调用多重继承函数的问题
Jun 26 Python
python3发送request请求及查看返回结果实例
Apr 30 Python
Python如何发送与接收大型数组
Aug 07 Python
浅谈Selenium 控制浏览器的常用方法
Dec 04 Python
Python图像处理之图像拼接
Apr 28 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
咖啡豆要不要放冰箱的原因
2021/03/04 冲泡冲煮
PHP缩略图生成和图片水印制作
2017/01/07 PHP
PHP的PDO事务与自动提交
2019/01/24 PHP
Laravel框架路由管理简单示例
2019/05/07 PHP
JavaScript URL参数读取改进版
2009/01/16 Javascript
Javascript select下拉框操作常用方法
2009/11/09 Javascript
CSS和JS标签style属性对照表(方便js开发的朋友)
2010/11/11 Javascript
jQuery学习笔记(1)--用jQuery实现异步通信(用json传值)具体思路
2013/04/08 Javascript
js实现div层缓慢收缩与展开的方法
2015/05/11 Javascript
JavaScript实现LI列表数据绑定的方法
2015/08/04 Javascript
javascript实现在指定元素中垂直水平居中
2015/09/13 Javascript
javascript 中关于array的常用方法详解
2017/05/05 Javascript
Nodejs模块载入运行原理
2018/02/23 NodeJs
浅谈JavaScript 代码整洁之道
2018/10/23 Javascript
详解微信小程序文件下载--视频和图片
2019/04/24 Javascript
在 Vue 中使用 JSX 及使用它的原因浅析
2020/02/10 Javascript
解决Vue项目中tff报错的问题
2020/10/21 Javascript
[03:17]DOTA2英雄基础教程 剧毒术士
2013/12/12 DOTA
Python用61行代码实现图片像素化的示例代码
2018/12/10 Python
Flask模板引擎之Jinja2语法介绍
2019/06/26 Python
python常用库之NumPy和sklearn入门
2019/07/11 Python
基于python全局设置id 自动化测试元素定位过程解析
2019/09/04 Python
python实现代码统计程序
2019/09/19 Python
Python键鼠操作自动化库PyAutoGUI简介(小结)
2020/05/17 Python
Python脚本实现监听服务器的思路代码详解
2020/05/28 Python
python报错: 'list' object has no attribute 'shape'的解决
2020/07/15 Python
Python 使用双重循环打印图形菱形操作
2020/08/09 Python
pandas将list数据拆分成行或列的实现
2020/12/13 Python
详解CSS的border边框属性及其在CSS3中的新特性
2016/05/10 HTML / CSS
销售副总经理岗位职责
2013/12/11 职场文书
综合办公室个人的自我评价
2013/12/22 职场文书
学校党员干部承诺书
2015/05/04 职场文书
CentOS8.4安装Redis6.2.6的详细过程
2021/11/20 Redis
Win11 Build 22000.51版本文件资源管理器“命令栏”和上下文菜单有什么新变化?
2021/11/21 数码科技
http通过StreamingHttpResponse完成连续的数据传输长链接方式
2022/02/12 Python
Python+Tkinter制作专属图形化界面
2022/04/01 Python