1 class PageHelper: 2 def __init__(self, count, page, per_num=10, show_page=11): 3 # 总数 4 self.count = count 5 # 当前的页码 6 self.page = page 7 # 每页的个数 8 self.per_num = per_num 9 if show_page % 2: 10 self.show_page = show_page 11 else: 12 self.show_page = show_page + 1 13 14 self.pagelist = [] 15 16 self.pagelist.append(" <a href=‘/classes?p=1‘>第一页</a>") 17 if page <= 1: 18 self.pagelist.append(" <a href=‘javascript: void(0)‘>上一页</a>") 19 else: 20 self.pagelist.append(" <a href=‘/classes?p={}‘>上一页</a>".format(self.page - 1)) 21 22 def pagestr(self): 23 startpage = 1 24 endpage = 1 25 step = self.show_page // 2 26 # 确定分多少页 27 b, r = divmod(self.count, self.per_num) 28 if r: 29 # 如果余数不为0,页数加1 30 b += 1 31 if b <= self.show_page: 32 startpage = 1 33 endpage = b 34 elif self.page - step <= 1: 35 startpage = 1 36 endpage = self.show_page 37 elif self.page + step >= b: 38 startpage = b - self.show_page + 1 39 endpage = b 40 else: 41 startpage = self.page - step 42 endpage = self.page + step 43 44 for i in range(startpage, endpage + 1): 45 if self.page == i: 46 self.pagelist.append(" <a href=‘/classes?p={}‘ style=‘background-color: aqua‘>{}</a>".format(i, i)) 47 else: 48 self.pagelist.append(" <a href=‘/classes?p={}‘ >{}</a>".format(i, i)) 49 50 if self.page >= b: 51 self.pagelist.append(" <a href=‘javascript: void(0)‘>下一页</a>") 52 else: 53 self.pagelist.append(" <a href=‘/classes?p={}‘>下一页</a>".format(self.page + 1)) 54 self.pagelist.append(" <a href=‘/classes?p={}‘>尾页</a>".format(b)) 55 56 return "".join(self.pagelist)