代码:

from django.utils.safestring import mark_safe

class Paginator(object):

	def __init__(self,current_page,total_item_count,base_url,per_page_count=10,show_pager_count=11):
"""
:param current_page: 当前页码
:param total_item_count: 数据库数据总条数
:param base_url: 分页前缀URL
:param per_page_count: 每页显示数据条数
:param show_pager_count: 对多显示的页码
"""
self.current_page = current_page
self.total_item_count = total_item_count
self.base_url = base_url
self.per_page_count = per_page_count
self.show_pager_count = show_pager_count #获取页码数及最后一页上显示的条目数
max_pager_num, b = divmod(total_item_count, per_page_count) if b:
max_pager_num += 1
self.max_pager_num = max_pager_num @property
def start(self):
"""
#每一页显示的第一条数据
:return:
"""
return (self.current_page-1)* self.per_page_count @property
def end(self):
"""
#每一页显示的最后一条数据
:return:
"""
return self.current_page * self.per_page_count def page_html(self):
""" :return:
"""
page_list = [] #如果当前页为第1页,则上一页按钮不可用
if self.current_page == 1:
prev = ' <li><a href="#">上一页</a></li>'
else:
prev = ' <li><a href="%s?page=%s">上一页</a></li>' % (self.base_url,self.current_page - 1,)
page_list.append(prev) half_show_pager_count = int(self.show_pager_count / 2) # 页面显示的总页数小于定义的页面上显示的页数时
if self.max_pager_num < self.show_pager_count:
pager_start = 1
pager_end = self.max_pager_num + 1
else:
#当前页码数小于定义的页面显示的页数的一半时
if self.current_page <= half_show_pager_count:
pager_start = 1
pager_end = self.show_pager_count + 1
else:
#当前面码数大于定义的页面显示的页数的一半时
if self.current_page + half_show_pager_count > self.max_pager_num:
pager_start = self.max_pager_num - self.show_pager_count + 1
pager_end = self.max_pager_num + 1
else:
#正常显示的时候
pager_start = self.current_page - half_show_pager_count
pager_end = self.current_page + half_show_pager_count + 1 #遍历循环当前页的每一条记录
for i in range(pager_start, pager_end):
if i == self.current_page:
tpl = ' <li class="active"><a href="%s?page=%s">%s</a></li>' % (self.base_url,i, i,)
else:
tpl = ' <li><a href="%s?page=%s">%s</a></li>' % (self.base_url,i, i,)
page_list.append(tpl) # 如果当前页为最后一页,则下一页按钮不可用
if self.current_page == self.max_pager_num:
next = ' <li><a href="#">下一页</a></li>'
else:
next = ' <li><a href="%s?page=%s">下一页</a></li>' % (self.base_url,self.current_page + 1,)
page_list.append(next) return mark_safe(''.join(page_list))

使用方法:

从浏览器中取出当前的页码数

current_page=int(request.GET.get("page",1))

从数据库中取出的总的记录数

item_list=models.数据表.objects.all()
total_item_count=item_list.count()

使用Paginator类实例化一个页码的对象:

page_obj=Paginator(current_page,total_item_count,"/index/")
需要注意的是:

    实例化page_obj的时候,可以定义每页显示的记录个数per_page_count及显示在页面上的页码的个数show_page_count

    每页显示的记录数per_page_count默认值为10,
页面显示的页码的个数show_page_count默认值为11

定义返回到客户端的记录列表

item_list=models.数据表.objects.all()[page_obj.start:page_obj.end]

最后使用render或者redirect返回给客户端

return render(request,"aaa.html",{"item_list":item_list,"page_html":page_obj.page_html()})

最新文章

  1. 【leetcode】Bitwise AND of Numbers Range(middle)
  2. Eclipse程序员要掌握的常用快捷键
  3. 用C++,调用浏览器打开一个网页
  4. 服务器的Arch Linux,CentOS的,Debian的,Fedora的,Gentoo的,openSUSE的,Slackware的,和Ubuntu哪个好
  5. asp.net 计算两个时间差
  6. .NET类型转换的常用方式
  7. ORACLE 修改日志大小及增加日志成员
  8. 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include &quot;StdAfx.h&quot;”?
  9. hadoop实例
  10. 奔小康赚大钱(km)
  11. 2014年10本月微软MVP应用程序启动!
  12. 20130620—ant和java杂学随笔
  13. Android 一个改善的okHttp封装库
  14. TCP协议之三次握手与四次挥手
  15. typeof、constructor和instance
  16. 菜鸟之旅——.NET垃圾回收机制
  17. Hive函数:CUME_DIST,PERCENT_RANK
  18. web服务器之nginx和apache的区别
  19. CentOS运维常用技能
  20. C#窗体打包步骤

热门文章

  1. 【Tomcat】batch获得war包
  2. i++,++i 作为参数
  3. NodeJS入门简介
  4. 4_CSRF
  5. What is the difference between Debug and Release in Visual Studio?
  6. python 之 计数器(counter)
  7. Ext:ComboBox实战
  8. 散列表(拉链法与线性探测法)Java实现
  9. C++学习日记(一)————类与对象
  10. ue4音效、动画结合实例