自定义分页器
Posted jixu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义分页器相关的知识,希望对你有一定的参考价值。
class PageInfo: def __init__(self, num_page, total, uri, pei_page=10, show_page=11): if num_page.isdigit(): num_page = int(num_page) else: num_page = 1 self.num_page = num_page self.pei_page = pei_page self.show_page = show_page self.uri = uri a, b = divmod(total, pei_page) self.total_page = a if b: self.total_page += 1 def get_start(self): return (self.num_page - 1) * 10 def get_stop(self): return self.num_page * 10 def page(self): args = [] if self.num_page - (self.show_page // 2) < 1: start = 1 end = self.show_page + 1 elif self.num_page + (self.show_page // 2) > self.total_page: start = self.total_page - self.show_page end = self.total_page + 1 else: start = self.num_page - (self.show_page // 2) end = self.num_page + (self.show_page // 2) + 1 print(end) if self.num_page - 1 > 0: s = "<li><a href=‘/%s%s‘>上一页</a></li>" % (self.uri, self.num_page - 1) else: s = "<li class=‘disabled‘><a href=‘#‘ >上一页</a></li>" args.append(s) for i in range(start, end): if i == self.num_page: s = "<li class=‘active‘><a href=‘/%s%s‘>%s </a></li>" % (self.uri, i, i) else: s = "<li><a href=‘/%s%s‘>%s </a></li>" % (self.uri, i, i) args.append(s) if self.num_page + 1 > self.total_page: s = "<li class=‘disabled‘><a href=‘#‘ >下一页</a></li>" else: s = "<li><a href=‘/%s%s‘ >下一页</a></li>" % (self.uri, self.num_page + 1) args.append(s) return "".join(args)
以上是关于自定义分页器的主要内容,如果未能解决你的问题,请参考以下文章