Python Django 页面上展示固定的页码数实现代码


Posted in Python onAugust 21, 2019

如果页数太多的话,全部显示在页面上就会显得很冗杂

Python Django 页面上展示固定的页码数实现代码

可以在页面中显示规定的页码数

例如:

Python Django 页面上展示固定的页码数实现代码

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" >
</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>
        <a href="#" rel="external nofollow" rel="external nofollow" aria-label="Previous">
          <span aria-hidden="true">«</span>
        </a>
      </li>
      <li>
        {{ page_html|safe }}
      </li>
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" aria-label="Next">
          <span aria-hidden="true">»</span>
        </a>
      </li>
    </ul>
  </nav> 
</div> 
</body>
</html>

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 = []
  for i in range(page_start, page_end+1):
    tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" >{0}</a></li>'.format(i)
    html_list.append(tmp)
 
  page_html = "".join(html_list)
 
  return render(request, "book_list.html", {"books": all_book, "page_html": page_html})

运行结果:

Python Django 页面上展示固定的页码数实现代码

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

Python 相关文章推荐
Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
Jul 02 Python
视觉直观感受若干常用排序算法
Apr 13 Python
Python开发的十个小贴士和技巧及长常犯错误
Sep 27 Python
python PrettyTable模块的安装与简单应用
Jan 11 Python
Python (Win)readline和tab补全的安装方法
Aug 27 Python
Django REST framework 单元测试实例解析
Nov 07 Python
Python使用Tkinter实现滚动抽奖器效果
Jan 06 Python
python requests.get带header
May 05 Python
使用Python文件读写,自定义分隔符(custom delimiter)
Jul 05 Python
简单的命令查看安装的python版本号
Aug 28 Python
Python爬虫抓取论坛关键字过程解析
Oct 19 Python
Python数据分析入门之数据读取与存储
May 13 Python
详解Python利用random生成一个列表内的随机数
Aug 21 #Python
Python Django 封装分页成通用的模块详解
Aug 21 #Python
Django之编辑时根据条件跳转回原页面的方法
Aug 21 #Python
python numpy 常用随机数的产生方法的实现
Aug 21 #Python
在django模板中实现超链接配置
Aug 21 #Python
python爬虫 批量下载zabbix文档代码实例
Aug 21 #Python
Django 在iframe里跳转顶层url的例子
Aug 21 #Python
You might like
PHP数组传递是值传递而非引用传递概念纠正
2013/01/31 PHP
php实现转换html格式为文本格式的方法
2016/05/16 PHP
laravel 解决Validator使用中出现的问题
2019/10/25 PHP
php设计模式之迭代器模式实例分析【星际争霸游戏案例】
2020/04/07 PHP
jQuery中after的两种用法实例
2013/07/03 Javascript
jquery遍历数组与筛选数组的方法
2013/11/05 Javascript
使用JS CSS去除IE链接虚线框的三种方法
2013/11/14 Javascript
JS实现的网页倒计时数字时钟效果
2015/03/02 Javascript
AngularJS教程之MVC体系结构详解
2016/08/16 Javascript
JS实现的手机端精简幻灯片效果
2016/09/05 Javascript
JavaScript获取当前时间向前推三个月的方法示例
2017/02/04 Javascript
JS实现非首屏图片延迟加载的示例
2018/01/06 Javascript
JS使用Date对象实时显示当前系统时间简单示例
2018/08/23 Javascript
Vue中插入HTML代码的方法
2018/09/21 Javascript
微信小程序实现联动选择器
2019/02/15 Javascript
vue输入节流,避免实时请求接口的实例代码
2019/10/30 Javascript
vue使用自定义事件的表单输入组件用法详解【日期组件与货币组件】
2020/06/01 Javascript
Python中AND、OR的一个使用小技巧
2015/02/18 Python
常见的在Python中实现单例模式的三种方法
2015/04/08 Python
Python的Flask框架与数据库连接的教程
2015/04/20 Python
Python实现数通设备端口使用情况监控实例
2015/07/15 Python
Python编码爬坑指南(必看)
2016/06/10 Python
python 矩阵增加一行或一列的实例
2018/04/04 Python
Django Rest framework解析器和渲染器详解
2019/07/25 Python
简单了解为什么python函数后有多个括号
2019/12/19 Python
基于python实现把json数据转换成Excel表格
2020/05/07 Python
微信浏览器左上角返回按钮拦截功能
2017/11/21 HTML / CSS
手工制作的音乐盒:Music Box Attic
2019/09/05 全球购物
美国亚洲时尚和美容产品的一站式网上商店:Stylevana
2019/09/05 全球购物
航海技术专业毕业生推荐信
2014/07/09 职场文书
中学推普周活动总结
2015/05/07 职场文书
党员理论学习心得体会
2016/01/21 职场文书
如何书写民事调解协议书?
2019/06/25 职场文书
《成长的天空》读后感3篇
2019/12/06 职场文书
详解python字符串驻留技术
2021/05/21 Python
Mysql外键约束的创建与删除的使用
2022/03/03 MySQL