#coding:utf8
#!/usr/bin/env python
__author__ = 'sws'
from django.utils.html import mark_safe
class PagiInfo:
'''
分页信息
确定每页有几条信息 per_item
确定每页的显示开始的信息序号,以及结束序号
self.Page:当前页
self.Total_count:信息总数目
self.Per_item:每页显示的数目 默认为5
'''
def __init__(self, page, total_count, per_item=5):
self.Page = page
self.Total_count = total_count
self.Per_item = per_item
@property
def start(self):
'''
返回开始的序号
'''
return self.Per_item*(self.Page-1)
@property
def end(self):
'''
返回结束的序号
'''
return self.Per_item*(self.Page)
@property
def total_pages(self):
'''
返回总页数
'''
pages = divmod(self.Total_count,self.Per_item)
if pages[1] != 0:
total_page = pages[0]+1
else:
total_page = pages[0]
return total_page
def Paginor(page, total_pages):
'''
:param page: 当前页
:param total_pages: 总页数
:return: 分页字符串
'''
if total_pages < 9:
start = 0;
end = total_pages
else:
if page < 5:
start = 0
end = 10
else:
start = page - 5
if page + 4 > total_pages:
end = total_pages
else:
end = page+4
pa_html = ['<a href=/index/1>首页</a>']
if page <= 1:
pa_html.append('<a href=#>前一页</a>')
else:
pa_html.append('<a href=/index/%d>前一页</a>' %(page-1))
for i in range(start+1,end+1):
temp = '<a href=/index/%d>%d</a>' %(i, i)
pa_html.append(temp)
if page >= total_pages:
pa_html.append('<a href=#>后一页</a>')
else:
pa_html.append('<a href=/index/%d>后一页</a>' %(page+1))
pa_html.append('<a href=/index/%d>尾页</a>' %(total_pages+1))
page_string=mark_safe(' '.join(pa_html))
return page_string