python 分页 封装
分页 封装
我是在项目根目录创建个分页文件
分页代码:
class Pagination(object): def __init__(self, data_num, current_page, url_prefix, per_page=10, max_show=11):
"""
进行初始化.
:param data_num: 数据总数
:param current_page: 当前页
:param url_prefix: 生成的页码的链接前缀
:param per_page: 每页显示多少条数据
:param max_show: 页面最多显示多少个页码
"""
self.data_num = data_num
self.per_page = per_page
self.max_show = max_show
self.url_prefix = url_prefix # 把页码数算出来
self.page_num, more = divmod(data_num, per_page)
if more:
self.page_num += 1 try:
self.current_page = int(current_page)
except Exception as e:
self.current_page = 1
# 如果URL传过来的页码数是负数
if self.current_page <= 0:
self.current_page = 1
# 如果URL传过来的页码数超过了最大页码数
elif self.current_page > self.page_num:
self.current_page = self.page_num # 默认展示最后一页 # 页码数的一半 算出来
self.half_show = max_show // 2 # 页码最左边显示多少
if self.current_page - self.half_show <= 1:
self.page_start = 1
self.page_end = self.max_show
elif self.current_page + self.half_show >= self.page_num: # 如果右边越界
self.page_end = self.page_num
self.page_start = self.page_num - self.max_show
else:
self.page_start = self.current_page - self.half_show
# 页码最右边显示
self.page_end = self.current_page + self.half_show @property
def start(self):
# 数据从哪儿开始切
return (self.current_page - 1) * self.per_page @property
def end(self):
# 数据切片切到哪儿
return self.current_page * self.per_page def page_html(self):
# 生成页码
l = []
# 加一个首页
l.append('<li><a href="{}?page=1">首页</a></li>'.format(self.url_prefix))
# 加一个上一页
if self.current_page == 1:
l.append('<li class="disabled" ><a href="#">«</a></li>'.format(self.current_page))
else:
l.append('<li><a href="{}?page={}">«</a></li>'.format(self.url_prefix, self.current_page - 1))
for i in range(self.page_start, self.page_end + 1): if i == self.current_page:
tmp = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(self.url_prefix, i)
else:
tmp = '<li><a href="{0}?page={1}">{1}</a></li>'.format(self.url_prefix, i)
l.append(tmp) # 加一个下一页
if self.current_page == self.page_num:
l.append('<li class="disabled"><a href="#">»</a></li>'.format(self.current_page))
else:
l.append('<li><a href="{}?page={}">»</a></li>'.format(self.url_prefix, self.current_page + 1))
# 加一个尾页
l.append('<li><a href="{}?page={}">尾页</a></li>'.format(self.url_prefix, self.page_num))
return "".join(l)
分页
分页调用:
# 出版社
@check_login
def publishing_list(request):
pubishing = Press.objects.all() # 取出数据库数据
count_s = pubishing.count() # 统计出版社有多少数据
current_page = request.GET.get("page", 1) # 获取当前页 找不到返回1
from utils import mypage # 导入分页文件
obj = mypage.Pagination(count_s, current_page, "/publishing_list/") # 把数据传进去
pubishing_lists = pubishing[obj.start:obj.end]
page_html = obj.page_html() # 分页 的导航的 html 类似:上一页 1 2 3 4 5 6 下一页
return render(request, "publishing_list.html", {"pubishing_lists": pubishing_lists, "page_html": page_html})
分页调用
分页html:
python 分页 封装的更多相关文章
- 第二百六十七节,Tornado框架-分页封装模块
Tornado框架-分页封装模块 框架引擎 #!/usr/bin/env python #coding:utf-8 import tornado.ioloop import tornado.web # ...
- 将Python脚本封装成exe可执行文件 转
将Python脚本封装成exe可执行文件 http://www.cnblogs.com/renzo/archive/2012/01/01/2309260.html cx_freeze是用来将 Pyt ...
- python继承——封装
python继承--封装 1 为什么要封装 封装数据的主要原因是:保护隐私 封装方法的主要原因是:隔离复杂度 2 封装分为两个层面 第一个层面的封装(什么都不用做):创建类和对象会分别创建二者的名称空 ...
- 【转】Python基础-封装与扩展、静态方法和类方法
[转]Python基础-封装与扩展.静态方法和类方法 一.封装与扩展 封装在于明确区分内外,使得类实现者可以修改封装内的东西而不影响外部调用者的代码:而外部使用者只知道一个接口(函数),只要接口(函数 ...
- Python分页转Mybatis pagehelper格式分页
最近工作里遇到一个需求要把之前用Java写的一个http接口替换成用Python写的,出参是带了mybatis pageHelper中PageInfo信息的一个JSON串,而Python这边分页不会涉 ...
- python 3 封装
python 3 封装 从封装本身的意思去理解,封装就好像是拿来一个麻袋,把小鱼,小虾,小王八,一起装进麻袋,然后把麻袋封上口子.照这种逻辑看,封装=‘隐藏’,这种理解是相当片面的. 先看如何隐藏 在 ...
- python面向对象-封装-property-接口-抽象-鸭子类型-03
封装 什么是封装: # 将复杂的丑陋的隐私的细节隐藏到内部,对外提供简单的使用接口 或 # 对外隐藏内部实现细节,并提供访问的接口 为什么需要封装 1.为了保证关键数据的安全性 2.对外部隐藏内部的实 ...
- python学习笔记:安装boost python库以及使用boost.python库封装
学习是一个累积的过程.在这个过程中,我们不仅要学习新的知识,还需要将以前学到的知识进行回顾总结. 前面讲述了Python使用ctypes直接调用动态库和使用Python的C语言API封装C函数, C+ ...
- python文件封装成*.exe
python文件封装成*.exe文件(单文件和多文件) 环境:win10 64位 python3.7 原文: https://www.cnblogs.com/jackzz/p/9431923.html ...
随机推荐
- MySQL 删除字段数据某关键字后的所有数据
),'开发商') WHERE Compay LIKE '%开发商%'; sql附上
- 剑指offer 04_替换字符串中的空格
#include <stdio.h> void ReplaceBlank(char string[],int length){ if(string == NULL || length == ...
- PHP如何将XML转成数组
如果你使用 curl 获取的 xml data $xml = simplexml_load_string($data); $data['tk'] = json_decode(json_encode($ ...
- 离散对数的求解(bsgs)
bsgs算法 主要用来解决${A^x} = B(\bmod C)$(c是质数),都是整数,已知A.B.C求x. 例:poj 2417 Discrete Logging 具体步骤如下: 先把$x = i ...
- Vue02 样式的动态绑定
daigengxin......2018-3-8 21:09:18 跟angular2类似,分为CSS类绑定和Style样式绑定两种方式,详情参见
- nexus admin 从文件角度进行密码重置
\sonatype-work\nexus\conf\security.xml 文件中保存账号密码信息. 打开 vi nexus-2.10.0-02-bundle\sonatype-work\nexus ...
- 算法Sedgewick第四版-第1章基础-024-M/M/1 queue
/****************************************************************************** * Compilation: javac ...
- setex()
设置值和有效期 $redis->setex($key, $expire, $value);//$expire,有效期,单位秒 相当于 SET key value//设置键值 EXPIRE key ...
- DBUtils工具类和DBCP连接池
今日内容介绍 1.DBUtils2.处理结果集的八种方式3.连接池4.连接池的用法1 PrepareStatement接口预编译SQL语句 1.1 预处理对象 * 使用PreparedStatemen ...
- mysql--约束条件
主键的测试 PRIMARY KEY(PRIMARY可以省略) --查看创建表的标的定义,可以查看主键 SHOW CREATE TABLE user1; ,'king');--主键不能重复 ,'kin ...