分页 封装

我是在项目根目录创建个分页文件

分页代码:

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:

最新文章

  1. 判断点是否落在面中的Oracle存储过程描述
  2. iOS开发之cell多按钮
  3. WaterfallTree(瀑布树) 详细技术分析系列
  4. Nginx与服务器集群在它的配置文件中的配置
  5. Python代码项目目录规范v1.0
  6. mysql中Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password:YES)(zhuan)
  7. MySQL索引背后的数据结构及算法原理 --转
  8. C++ operator 知识点 2
  9. 行列有序矩阵求第k大元素
  10. The required Server component failed to start so Tomcat is unable to start解决之一
  11. linux shell sleep/wait(转载)
  12. 老男孩Python全栈开发(92天全)视频教程 自学笔记01
  13. 【jQuery】 JQ和HTML以及JQ遍历元素
  14. WPF Grid布局
  15. SpringBoot企业级框架
  16. websevice动态控制访问ip
  17. 为什么研发团队不适合量化KPI的绩效考核?
  18. 转: Linux --- Supervisor的作用与配置
  19. [LearnOpenGL]照相机的变换、坐标系、摄像机
  20. Smali语法简单介绍

热门文章

  1. eclipse中maven的配置与使用
  2. jQuery选择器大全整理
  3. cocos2d-js 骨骼动画 3.10
  4. Excel VBA 若要在64位系统上使用,则必须更新此项目中的代码,请检查并更新Declare语句,然后用PtrSafe属性标记它们
  5. javascript使用setTimeout、setInterval时找不到变量的问题
  6. Bulma 中的媒体查询
  7. 与HDFS交互- By web界面
  8. 自定义非等高 Cell
  9. 不准使用xib自定义控制器view的大小
  10. 算法训练 数字三角形(DP)