Django之分页
一、Django内置分页
rom django.shortcuts import render from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger # 分页组件要导入的模块 USER_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') # Paginator对象 paginator = Paginator(USER_LIST, 10) # 第一个参数是数据源,第二个参数是每页显示多少 # per_page: 每页显示条目数量 # count: 数据总个数 # num_pages:总页数 # page_range:总页数的索引范围,如: (1,10),(1,200) # page: page对象 try: # page对象有的方法 posts = paginator.page(current_page) # has_next 是否有下一页 # next_page_number 下一页页码 # has_previous 是否有上一页 # previous_page_number 上一页页码 # object_list 分页之后的数据列表,已经切片好的数据 # number 当前页 # paginator paginator对象 except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) return render(request,'index.html',{'posts':posts})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for row in posts.object_list %} <li>{{ row.name }}-{{ row.age }}</li> {% endfor %} </ul> {% if posts.has_previous %} <a href="/index.html?p={{ posts.previous_page_number }}">上一页</a> {% else %} <a href="#">上一页</a> {% endif %} <span> {{ posts.number }} - {{ posts.paginator.num_pages }} </span> {% if posts.has_next %} <a href="/index.html?p={{ posts.next_page_number }}">下一页</a> {% endif %} </body> </html>
二、自定义分页
class Pagination(object): def __init__(self,totalCount,currentPage,perPageItemNum=10,maxPageNum=7): self.total_count = totalCount # 所有数据的个数 try: v = int(currentPage) if v <= 0: v = 1 self.current_page = v # 当前页 except Exception as e: self.current_page = 1 self.per_page_item_num = perPageItemNum # 每页显示的行数 self.max_page_num = maxPageNum # 最多页面索引个数 def start(self): return (self.current_page-1)*self.per_page_item_num def end(self): return self.current_page*self.per_page_item_num @property # 可以让这个方法不需要加括号就能调用 def num_pages(self): # 总页数 a,b = divmod(self.total_count,self.per_page_item_num) if a == 0: return a return a+1 def page_num_range(self): # 每个页面要显示的页码范围 # self.current_page # 当前页 # self.per_pager_num #每页最多显示的页码数量 # self.num_pages # 总页数 # 总页数小于每页最多显示的页码数 if self.num_pages < self.max_page_num: return range(1, self.num_pages + 1) # 总页数很多 part = int(self.max_page_num / 2) if self.current_page <= part: return range(1, self.max_page_num + 1) if (self.current_page + part) > self.num_pages: return range(self.num_pages - self.max_page_num + 1, self.num_pages + 1) return range(self.current_page - part, self.current_page + part + 1) # 返回要显示的页码数 def page_str(self): page_list = [] first = "<a href='/index.html?p=1'>首页</a>" page_list.append(first) if self.current_page == 1: prev = "<a href='#'>上一页</a>" else: prev = "<a href='/index.html?p=%s'>上一页</a>" %(self.current_page-1) page_list.append(prev) # 添加 上一页 for i in self.page_num_range(): # 添加页码 if i == self.current_page: temp = "<a href='/index.html?p=%s'>%s</a>" %(i,i) else: temp = "<a href='/index.html?p=%s'>%s</a>" %(i,i) page_list.append(temp) if self.current_page == self.num_pages: nex = "<a href='#'>下一页</a>" else: nex = "<a href='/index.html?p=%s'>下一页</a>" % (self.current_page + 1) page_list.append(nex) last = "<a href='/index.html?p=%s'>尾页</a>" %self.num_pages page_list.append(last) return ' '.join(page_list)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for row in data %} <li>{{ row.name }}-{{ row.age }}</li> {% endfor %} </ul> {#页码#} {% for i in page_obj.page_num_range %} <a href="/index.html?p={{ i }}">{{ i }}</a> {% endfor %} <hr> {{ page_obj.page_str|safe }} </body> </html>
from django.shortcuts import render from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger from app01.pager import Pagination # 导入这个自定制的模块 USER_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') page_obj = Pagination(666,current_page) # 数据从数据库取 data_list = USER_LIST[page_obj.start():page_obj.end()] return render(request,'index.html',{'data':data_list,'page_obj':page_obj})