写一段python代码_python写的一段分页的代码

代码:

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 = '

上一页'

else:

prev = '

上一页' % (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 = '

%s' % (self.base_url,i, i,)

else:

tpl = '

%s' % (self.base_url,i, i,)

page_list.append(tpl)

# 如果当前页为最后一页,则下一页按钮不可用

if self.current_page == self.max_pager_num:

next = '

下一页'

else:

next = '

下一页' % (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()})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值