Custom pager
class Pagination (Object): DEF the __init__ (Self, current_page, ALL_COUNT are, per_page_num = 2, pager_count =. 11 ): "" " Package tab data : param current_page: this page : param all_count: Total number of stripes in the database : param the number of data per page is displayed: per_page_num : param pager_count: up to displayed page number usage: QuerySet = model.objects.all () page_obj = Pagination (current_page, ALL_COUNT are) page_data = QuerySet [page_obj.start: page_obj.end] acquiring data instead of using the original page_data queryset retrieve a front tab style with page_obj.page_html "" " the try : current_page = int(current_page) except Exception as e: current_page = 1 if current_page <1: current_page = 1 self.current_page = current_page self.all_count = all_count self.per_page_num = per_page_num # 总页码 all_pager, tmp = divmod(all_count, per_page_num) if tmp: all_pager += 1 self.all_pager = all_pager self.pager_count = pager_count self.pager_count_half = int ((pager_count -. 1) / 2 ) @Property DEF Start (Self): return (self.current_page -. 1) * self.per_page_num @Property DEF End (Self): return self.current_page * self.per_page_num DEF page_html (Self): # if the total p <11: IF self.all_pager <= self.pager_count: pager_start =. 1 pager_end = self.all_pager. 1 + # total p>. 11 the else : # up on this page if <= page display a page number 11/2 if self.current_page <= self.pager_count_half: pager_start = 1 pager_end = self.pager_count + 1 # 当前页大于5 else: # 页码翻到最后 if (self.current_page + self.pager_count_half) > self.all_pager: pager_end = self.all_pager + 1 pager_start = self.all_pager - self.pager_count + 1 else: pager_start = self.current_page - self.pager_count_half pager_end = self.current_page + self.pager_count_half + 1 page_html_list = [] # 添加前面的nav和ul标签 page_html_list.append(''' <nav aria-label='Page navigation>' <ul class='pagination'> ''') first_page = '<li><a href="?page=%s">首页</a></li>' % (1) page_html_list.append(first_page) if self.current_page <= 1: prev_page = '<li class="disabled"><a href="# "> Previous </a> </ Li> prev_pagethe else' := '<li><a href="?page=%s">上一页</a></li>' % (self.current_page - 1,) page_html_list.append(prev_page) for i in range(pager_start, pager_end): if i == self.current_page: temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i,) else: temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,) page_html_list.append(temp) if self.current_page >= self.all_pager: next_page = '<li class="disabled"><a href="#">下一页</a></li>' else: next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page + 1,) page_html_list.append(next_page) last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,) page_html_list.append(last_page) # 尾部添加标签 page_html_list.append(''' </nav> </ul> ''') return ''.join(page_html_list)
class Pagination (Object): DEF the __init__ (Self, current_page, ALL_COUNT are, per_page_num = 2, pager_count =. 11 ): "" " Package tab data : param current_page: this page : param all_count: Total number of stripes in the database : param the number of data per page is displayed: per_page_num : param pager_count: up to displayed page number usage: QuerySet = model.objects.all () page_obj = Pagination (current_page, ALL_COUNT are) page_data = QuerySet [page_obj.start: page_obj.end] acquiring data instead of using the original page_data queryset retrieve a front tab style with page_obj.page_html "" " the try : current_page = int(current_page) except Exception as e: current_page = 1 if current_page <1: current_page = 1 self.current_page = current_page self.all_count = all_count self.per_page_num = per_page_num # 总页码 all_pager, tmp = divmod(all_count, per_page_num) if tmp: all_pager += 1 self.all_pager = all_pager self.pager_count = pager_count self.pager_count_half = int ((pager_count -. 1) / 2 ) @Property DEF Start (Self): return (self.current_page -. 1) * self.per_page_num @Property DEF End (Self): return self.current_page * self.per_page_num DEF page_html (Self): # if the total p <11: IF self.all_pager <= self.pager_count: pager_start =. 1 pager_end = self.all_pager. 1 + # total p>. 11 the else : # up on this page if <= page display a page number 11/2 if self.current_page <= self.pager_count_half: pager_start = 1 pager_end = self.pager_count + 1 # 当前页大于5 else: # 页码翻到最后 if (self.current_page + self.pager_count_half) > self.all_pager: pager_end = self.all_pager + 1 pager_start = self.all_pager - self.pager_count + 1 else: pager_start = self.current_page - self.pager_count_half pager_end = self.current_page + self.pager_count_half + 1 page_html_list = [] # 添加前面的nav和ul标签 page_html_list.append(''' <nav aria-label='Page navigation>' <ul class='pagination'> ''') first_page = '<li><a href="?page=%s">首页</a></li>' % (1) page_html_list.append(first_page) if self.current_page <= 1: prev_page = '<li class="disabled"><a href="# "> Previous </a> </ Li> prev_pagethe else' := '<li><a href="?page=%s">上一页</a></li>' % (self.current_page - 1,) page_html_list.append(prev_page) for i in range(pager_start, pager_end): if i == self.current_page: temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i,) else: temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,) page_html_list.append(temp) if self.current_page >= self.all_pager: next_page = '<li class="disabled"><a href="#">下一页</a></li>' else: next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page + 1,) page_html_list.append(next_page) last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,) page_html_list.append(last_page) # 尾部添加标签 page_html_list.append(''' </nav> </ul> ''') return ''.join(page_html_list)