使用Python自定义分页组件 本博客转自 Iced的博客 http://blog.icedll.xyz/index.php/archives/12/
class PageInfo(object):
def __init__(self,current_page,all_count,per_page,base_url,show_page):
"""
:param current_page: 当前页页码
:param all_count: 数据总行数
:param per_page: 每页显示行数
:param base_url: 分页页面的url
:param show_page: 页面上显示的页码数
"""
try:
self.current_page = int(current_page)
except Exception as e:
self.current_page = 1
self.per_page = per_page
a,b = divmod(all_count,per_page)
if b:
a = a+1
self.all_pager = a
self.show_page = show_page
self.base_url = base_url
def start(self):
return (self.current_page-1) * self.per_page
def end(self):
return self.current_page * self.per_page
def pager(self):
page_list = []
half = int((self.show_page-1)/2)
if self.all_pager < self.show_page:
begin = 1
stop = self.all_pager + 1
else:
if self.current_page <= half:
begin = 1
stop = self.show_page + 1
else:
if self.current_page + half > self.all_pager:
begin = self.all_pager - self.show_page + 1
stop = self.all_pager + 1
else:
begin = self.current_page - half
stop = self.current_page + half + 1
front = "<li><a href='%s?page=%s'>首页</a></li>"%(self.base_url,1)
page_list.append(front)
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)
for i in range(begin,stop):
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_list.append(temp)
if self.current_page >= self.all_pager:
nex = "<li><a href='#'>下一页</a></li>"
else:
nex = "<li><a href='%s?page=%s'>下一页</a></li>" % (self.base_url,self.current_page + 1)
page_list.append(nex)
back = "<li><a href='%s?page=%s'>尾页</a></li>"%(self.base_url,self.all_pager)
page_list.append(back)
return ''.join(page_list)
此组件为个人在实践项目中所开发的小组件,不同于模板引擎的分页组件。
|