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 创建空的list,以及append用法讲解
May 04 Python
Python3环境安装Scrapy爬虫框架过程及常见错误
Jul 12 Python
pytorch 输出中间层特征的实例
Aug 17 Python
python3 实现爬取TOP500的音乐信息并存储到mongoDB数据库中
Aug 24 Python
python被修饰的函数消失问题解决(基于wraps函数)
Nov 04 Python
15行Python代码实现免费发送手机短信推送消息功能
Feb 27 Python
Softmax函数原理及Python实现过程解析
May 22 Python
Python批量获取并保存手机号归属地和运营商的示例
Oct 09 Python
Python 按比例获取样本数据或执行任务的实现代码
Dec 03 Python
基于Python编写简易版的天天跑酷游戏的示例代码
Mar 23 Python
什么是Python装饰器?如何定义和使用?
Apr 11 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+javascript模拟Matrix画面
2006/10/09 PHP
php下实现农历日历的代码
2007/03/07 PHP
php删除指定目录的方法
2015/04/03 PHP
WordPress中给媒体文件添加分类和标签的PHP功能实现
2015/12/31 PHP
Yii2.0预定义的别名功能小结
2016/07/04 PHP
利用php-cli和任务计划实现订单同步功能的方法
2017/05/03 PHP
JS打印gridview实现原理及代码
2013/02/05 Javascript
Jquery实现点击切换图片并隐藏显示内容(2种方法实现)
2013/04/11 Javascript
JavaScript将相对地址转换为绝对地址示例代码
2013/07/19 Javascript
改变隐藏的input中value值的方法
2014/03/19 Javascript
javascript引用赋值(地址传值)用法实例
2015/01/13 Javascript
javascript事件冒泡简单示例
2016/06/20 Javascript
使用 Vue.js 仿百度搜索框的实例代码
2017/05/09 Javascript
node.js支持多用户web终端实现及安全方案
2017/11/29 Javascript
koa源码中promise的解读
2018/11/13 Javascript
vuex页面刷新后数据丢失的方法
2019/01/17 Javascript
vue仿ios列表左划删除
2019/09/26 Javascript
JavaScript函数IIFE使用详解
2019/10/21 Javascript
python通过ftplib登录到ftp服务器的方法
2015/05/08 Python
Python与Java间Socket通信实例代码
2017/03/06 Python
django定期执行任务(实例讲解)
2017/11/03 Python
python使用itchat实现手机控制电脑
2018/02/22 Python
使用python 打开文件并做匹配处理的实例
2019/01/02 Python
python 根据时间来生成唯一的字符串方法
2019/01/14 Python
numpy.random.shuffle打乱顺序函数的实现
2019/09/10 Python
解决Tensorboard 不显示计算图graph的问题
2020/02/15 Python
Move Free官方海外旗舰店:美国骨关节健康专业品牌
2017/12/06 全球购物
微软新西兰官方网站:Microsoft New Zealand
2018/08/17 全球购物
毕业生自我推荐
2013/11/04 职场文书
职专应届生求职信
2013/11/16 职场文书
实习鉴定评语
2014/01/19 职场文书
优秀经理获奖感言
2014/03/04 职场文书
知识改变命运演讲稿
2014/05/21 职场文书
市委常委会班子党的群众路线教育实践活动整改方案
2014/10/25 职场文书
经典导游欢迎词
2015/01/26 职场文书
公司安全管理制度范本
2015/08/05 职场文书