python自定义分页器()转自https://www.cnblogs.com/yuanchenqi/articles/7652353.html
""" 分页组件使用示例:obj = Pagination(request.GET.get('page',1),len(USER_LIST),request.path_info)page_user_list = USER_LIST[obj.start:obj.end]page_html = obj.page_html()return render(request,'index.html',{'users':page_user_list,'page_html':page_html})"""class Pagination(object):def __init__(self,current_page,all_count,base_url,per_page_num=2,pager_count=11):"""封装分页相关数据:param current_page: 当前页:param all_count: 数据库中的数据总条数:param per_page_num: 每页显示的数据条数:param base_url: 分页中显示的URL前缀:param pager_count: 最多显示的页码个数"""try:current_page = int(current_page)except Exception as e:current_page = 1if current_page <1:current_page = 1self.current_page = current_pageself.all_count = all_countself.per_page_num = per_page_numself.base_url = base_url# 总页码all_pager, tmp = divmod(all_count, per_page_num)if tmp:all_pager += 1self.all_pager = all_pagerself.pager_count = pager_countself.pager_count_half = int((pager_count - 1) / 2)@propertydef start(self):return (self.current_page - 1) * self.per_page_num@propertydef end(self):return self.current_page * self.per_page_numdef page_html(self):# 如果总页码 < 11个:if self.all_pager <= self.pager_count:pager_start = 1pager_end = self.all_pager + 1# 总页码 > 11else:# 当前页如果<=页面上最多显示11/2个页码if self.current_page <= self.pager_count_half:pager_start = 1pager_end = self.pager_count + 1# 当前页大于5else:# 页码翻到最后if (self.current_page + self.pager_count_half) > self.all_pager:pager_end = self.all_pager + 1pager_start = self.all_pager - self.pager_count + 1else:pager_start = self.current_page - self.pager_count_halfpager_end = self.current_page + self.pager_count_half + 1page_html_list = []first_page = '<li><a href="%s?page=%s">首页</a></li>' % (self.base_url,1,)page_html_list.append(first_page)if self.current_page <= 1:prev_page = '<li class="disabled"><a href="#">上一页</a></li>'else:prev_page = '<li><a href="%s?page=%s">上一页</a></li>' % (self.base_url,self.current_page - 1,)page_html_list.append(prev_page)for i in range(pager_start, pager_end):if i == self.current_page:temp = '<li class="active"><a href="%s?page=%s">%s</a></li>' % (self.base_url,i, i,)else:temp = '<li><a href="%s?page=%s">%s</a></li>' % (self.base_url,i, i,)page_html_list.append(temp)if self.current_page >= self.all_pager:next_page = '<li class="disabled"><a href="#">下一页</a></li>'else:next_page = '<li><a href="%s?page=%s">下一页</a></li>' % (self.base_url,self.current_page + 1,)page_html_list.append(next_page)last_page = '<li><a href="%s?page=%s">尾页</a></li>' % (self.base_url,self.all_pager,)page_html_list.append(last_page)return ''.join(page_html_list)
转载于:https://www.cnblogs.com/wangdamao/p/11568905.html
python自定义分页器()转自https://www.cnblogs.com/yuanchenqi/articles/7652353.html相关推荐
- urllib 模块 https://www.cnblogs.com/guishou/articles/7089496.html
1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=Fals ...
- struct和typedef struct彻底明白了,引用自https://www.cnblogs.com/qyaizs/articles/2039101.html
struct和typedef struct彻底明白了 struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: t ...
- python自定义库文件路径
各有各的小烦恼,各有的小期待 这是人家私事,不要大嘴巴 在Pycharm中import whois时,总是失败 原因是安装了python3.x 相关操作过程,将环境变量path中关于Python的配置 ...
- Web框架之Django_07 进阶操作(MTV与MVC、多对多表三种创建方式、前后端传输数据编码格式contentType、ajax、自定义分页器)
阅读目录 一.MVC与MTV 二.多对多表的创建 三.ajax,前后端传输编码格式contentType 四.批量插入数据与自定义分页器 摘要 MTV与MVC 多对多表三种创建方式 ajax ,前后端 ...
- python 自定义模块的发布和安装
[学习笔记] 自定义模块 使用的是pycharm 说白了就是.py文件都可以作为模块导入,像定义一个文件 名字为Mycode __all__ = ["add","sub ...
- Django分页器和自定义分页器
一.自定义分页器 import copyclass Pagination():def __init__(self,request,current_page,all_data_num,each_page ...
- Python自定义分页组件
为了防止XSS即跨站脚本攻击,需要加上 safe # 路由 from django.conf.urls import url from django.contrib import admin from ...
- Python: 自定义类对象序列化为Json串
之前已经实现了Python: Json串反序列化为自定义类对象,这次来实现了Json的序列化. 测试代码和结果如下: import Json.JsonToolclass Score:math = 0c ...
- TensorFlow使用Python自定义op和损失函数
TensorFlow使用Python自定义op和损失函数 TensorFlow是静态图结构,即必须把所有的操作以及网络结构定义好(后来有了动态图功能,即Eager Execution ),在没有用tf ...
最新文章
- 智能安防 回家开门进入“看脸”时代
- 统计dataframe中所有列的null数量与填充null注意事项
- 1. face_generate.py
- 计算机运维知识点,系统运维必会知识点
- 热电偶校验仪_热电偶校验方法_烟台一等热电偶,干湿两用温度校验炉公司
- C++中模板使用详解
- Ubuntu 想在 Windows 的 WSL 中做到领先
- Harris角点检测和Shi-Tomasi角点检测
- Qt4_中央窗口部件
- c语言数据结构用矩阵存储图,数据结构之---C语言实现图的数组(邻接矩阵)存储表示...
- 北邮计院13上机真题
- 功能测试分析和测试用例编写模板
- 用最简单的方式理解和使用ActivityMQ(基础入门)
- 《Android编程入门很简单》PDF版电子书下载
- c语言某年某月某日的天数,输入某年某月某日,判断这一天是这一年的第几天...
- 2017校招中的流年回忆
- 在Jetty中快速搭建SSL
- 企业寄件自定义短信通知教程
- 百万调音师—Audition多轨编辑
- uniapp 调用 android 原生sdk