分页第二版:
from django.utils.safestring import mark_safeclass Pagination(object): def __init__(self, current_page, data_count, per_page_count=10, pager_num=7): try: self.current_page = int(current_page) except Exception as e: self.current_page = 1 self.data_count = data_count self.per_page_count = per_page_count self.pager_num = pager_num @property def start(self): return (self.current_page - 1) * self.per_page_count @property def end(self): return self.current_page * self.per_page_count @property def total_count(self): v, y = divmod(self.data_count, self.per_page_count) if y: v += 1 return v def page_str(self, base_url): page_list = [] if self.total_count < self.pager_num: start_index = 1 end_index = self.total_count + 1 else: if self.current_page <= (self.pager_num + 1) / 2: start_index = 1 end_index = self.pager_num + 1 else: start_index = self.current_page - (self.pager_num - 1) / 2 end_index = self.current_page + (self.pager_num + 1) / 2 if (self.current_page + (self.pager_num - 1) / 2) > self.total_count: end_index = self.total_count + 1 start_index = self.total_count - self.pager_num + 1 if self.current_page == 1: prev = '
第一次在码云上面分享代码片段,记录一下:
https://gitee.com/trunkslisa/codes/14gkxi3zf9e2ulbvjnqyo90
class Pagination(object): def __init__(self, totalCount, currentPage, perPageNum=20, maxPageNum=7): # 传入: # -所有数据的个数 totalCount self.totalCount = totalCount # -当前页 currentPage try: v = int(currentPage) if v < 0: v = 1 self.currentPage = v except Exception as e: self.currentPage = 1 # -每页显示??行 perPageNum self.perPageNum = perPageNum # -最多显示??页 maxPageNum self.maxPageNum = maxPageNum # 开始条数 def start(self): return (self.currentPage - 1) * self.perPageNum # 结束条数 def end(self): return self.currentPage * self.perPageNum # 总页数 # 采用这个装饰器以后,本来需要用self.num_pages()这个方法的,后面的括号可以不用写了 @property def num_pages(self): # 取余数,不等于0的+1 a, b = divmod(self.totalCount, self.perPageNum) if b == 0: return a return a+1 # 根据情况生成相应的页码list def pager_num_range(self): # range应该根据当前页动态生成,自定制显示数量 # 代指当前页 #self.current_page # 最多显示的页码数量 #self.per_page_num # 总页数 #self.num_pages # 判断一下传入的页码是否大于总页数 if self.currentPage > self.num_pages: self.currentPage = self.num_pages # 总页数小于当前页 if self.num_pages < self.currentPage: return range(1, self.num_pages+1) # 总页数有很多 part = int(self.maxPageNum/2) #拿到显示总数量的一半 if self.currentPage <= part: return range(1, self.maxPageNum+1) # 判断当前页+part大于最后一页,显示最后一页至最后一页往前的per_pager_num的数量+1 if (self.currentPage + part) > self.num_pages: return range(self.num_pages - self.maxPageNum + 1, self.num_pages+1) return range(self.currentPage - part, self.currentPage + part + 1) def page_str(self): page_list = [] #首页 first = '
from app01.pager import Paginationfrom django.shortcuts import renderfrom django.core.paginator import Paginator, EmptyPage, PageNotAnIntegerUSER_LIST = []for i in range(1, 666): temp = { 'name': 'root' + str(i), 'age': i} USER_LIST.append(temp)def index(request): current_Page = request.GET.get('p') #这里的666是个模拟数据,实际使用中应用实际数量替换该位置 page_obj = Pagination(666, current_Page) #这里的USER_LIST是自己创建的模拟数据,主要为页面展示使用 data_list = USER_LIST[page_obj.start():page_obj.end()] return render(request, 'index3.html', { 'data': data_list, 'page_obj': page_obj})
Title
- { % for row in data %}
- { { row.name}}-{ { row.age }} { % endfor %}