🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ class Page(): def __init__(self, current_page, total_items, items_per_page=10, pagecode_to_show=11): self.current_page = current_page self.total_items = total_items self.items_per_page = items_per_page self.pagecode_to_show = pagecode_to_show @property def total_pages(self): total_pages, leave = divmod(self.total_items, self.items_per_page) if leave: total_pages = total_pages + 1 return total_pages @property def get_pages_list(self): total_pages = self.total_pages """总页数小于要显示页码,则全部显示""" if 0 < total_pages < self.pagecode_to_show: start_page_code = 1 end_page_code = total_pages + 1 else: """总页数大于要显示页码,则部分显示""" """处理首页部分超出范围情况""" if self.current_page <= (self.pagecode_to_show + 1) / 2: start_page_code = 1 end_page_code = self.pagecode_to_show + 1 elif total_pages - (self.pagecode_to_show + 1) / 2 < self.current_page <= total_pages: """处理尾页部分超出范围情况""" start_page_code = total_pages - self.pagecode_to_show + 1 end_page_code = total_pages + 1 else: """正常页码""" start_page_code = self.current_page - (self.pagecode_to_show - 1) / 2 end_page_code = self.current_page + (self.pagecode_to_show + 1) / 2 pages_list = [item for item in range(int(start_page_code), int(end_page_code))] return pages_list @property def get_prev_post_page(self): total_pages = self.total_pages if self.current_page == 1: prev_page = 1 post_page = self.current_page + 1 elif self.current_page == total_pages: prev_page = self.current_page - 1 post_page = total_pages else: prev_page = self.current_page - 1 post_page = self.current_page + 1 return prev_page, post_page @property def get_items_to_show(self): items_start = (self.current_page - 1) * self.items_per_page items_end = self.current_page * self.items_per_page + 1 return int(items_start), int(items_end) ~~~ ~~~ @auth def page(request): total_items = len(content) if request.method == 'GET': items_per_page = request.COOKIES.get('items_per_page') if items_per_page: try: items_per_page = int(items_per_page) except: items_per_page = 10 else: items_per_page = 10 current_page = int(request.GET.get('p', 1)) pg = Page(current_page, total_items, items_per_page) total_pages = pg.total_pages items_start, items_end = pg.get_items_to_show items_to_show = content[items_start:items_end] pages_list = pg.get_pages_list prev_page, post_page = pg.get_prev_post_page return render(request, 'page.html', { 'items_to_show': items_to_show, 'pages_list': pages_list, 'current_page': current_page, 'prev_page': prev_page, 'post_page': post_page, 'total_pages': total_pages, }) ~~~