django自定义分页器
一 django 的分页器
1 批量创建数据
批量导入数据: Booklist=[] for i in range(100): Booklist.append(Book(title="book"+str(i),price=30+i*i)) Book.objects.bulk_create(Booklist)
2 分页器的使用
book_list=Book.objects.all() paginator = Paginator(book_list, 10) print("count:",paginator.count) #数据总数 print("num_pages",paginator.num_pages) #总页数 print("page_range",paginator.page_range) #页码的列表 page1=paginator.page(1) #第1页的page对象 for i in page1: #遍历第1页的所有数据对象 print(i) print(page1.object_list) #第1页的所有数据 page2=paginator.page(2) print(page2.has_next()) #是否有下一页 print(page2.next_page_number()) #下一页的页码 print(page2.has_previous()) #是否有上一页 print(page2.previous_page_number()) #上一页的页码
抛错
page=paginator.page(12) # error:EmptyPage page=paginator.page("z") # error:PageNotAnInteger
3 view
from django.shortcuts import render,HttpResponse # Create your views here. from app01.models import * from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger book_list=Book.objects.all() paginator = Paginator(book_list, 10) page = request.GET.get('page',1) currentPage=int(page) try: print(page) book_list = paginator.page(page) except PageNotAnInteger: book_list = paginator.page(1) except EmptyPage: book_list = paginator.page(paginator.num_pages) return render(request,"index.html",{"book_list":book_list,"paginator":paginator,"currentPage":currentPage})
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container"> <h4>分页器</h4> <ul> {% for book in book_list %} <li>{{ book.title }} -----{{ book.price }}</li> {% endfor %} </ul> <ul class="pagination" id="pager"> {% if book_list.has_previous %} <li class="previous"><a href="/index/?page={{ book_list.previous_page_number }}">上一页</a></li> {% else %} <li class="previous disabled"><a href="#">上一页</a></li> {% endif %} {% for num in paginator.page_range %} {% if num == currentPage %} <li class="item active"><a href="/index/?page={{ num }}">{{ num }}</a></li> {% else %} <li class="item"><a href="/index/?page={{ num }}">{{ num }}</a></li> {% endif %} {% endfor %} {% if book_list.has_next %} <li class="next"><a href="/index/?page={{ book_list.next_page_number }}">下一页</a></li> {% else %} <li class="next disabled"><a href="#">下一页</a></li> {% endif %} </ul> </div> </body> </html>
提高 弹性显示页码
def index(request): book_list=Book.objects.all() paginator = Paginator(book_list, 15) page = request.GET.get('page',1) currentPage=int(page) # 如果页数十分多时,换另外一种显示方式 if paginator.num_pages>11: if currentPage-5<1: pageRange=range(1,11) elif currentPage+5>paginator.num_pages: pageRange=range(currentPage-5,paginator.num_pages+1) else: pageRange=range(currentPage-5,currentPage+5) else: pageRange=paginator.page_range try: print(page) book_list = paginator.page(page) except PageNotAnInteger: book_list = paginator.page(1) except EmptyPage: book_list = paginator.page(paginator.num_pages) return render(request,"index.html",locals())
二 自定义分页器源码
import copy class Paginator: def __init__(self,request,total_count,current_page=1,per_page_data_num=5,show_page_num=11): self.total_count=total_count #数据总数 self.per_page_data_num=per_page_data_num #每页的数据个数 5 self.show_page_num=show_page_num #显示页面个数 11 self.request=request self.params=self.get_params try: current_page = int(current_page) if current_page < 1: self.current_page = 1 elif current_page>self.get_page_num: self.current_page = 1 else: self.current_page = current_page except Exception as e: self.current_page = 1 self.half_num=self.show_page_num//2 @property def get_params(self): params = copy.deepcopy(self.request.GET) return params @property def get_page_num(self): p_num,mod=divmod(self.total_count,self.per_page_data_num) if mod: p_num+=1 return p_num @property def start(self): return self.per_page_data_num*(self.current_page-1) @property def end(self): return self.per_page_data_num*self.current_page def html(self): page_num_list=[] # print(self.get_page_num) #页码四种情况配置 if self.get_page_num<self.show_page_num: show_start=1 show_end=self.get_page_num+1 else: if self.current_page<=self.half_num: show_start = 1 show_end = self.show_page_num+1 elif self.current_page + self.half_num >= self.get_page_num: show_start = self.get_page_num - self.show_page_num + 1 show_end = self.get_page_num + 1 else: show_start=self.current_page-self.half_num show_end=self.current_page+self.half_num+1 # 首页 self.params['page']=1 first_page = r'<nav aria-label="Page navigation"><ul class="pagination"><li><a href="?%s">首页</a></li>' %(self.params.urlencode(),) page_num_list.append(first_page) # 上一页 if self.current_page == 1: previous_num = r'<li class="disabled"><a >上一页</a></li>' else: self.params['page'] = self.current_page-1 previous_num = r'<li><a href="?%s">上一页</a></li>' % (self.params.urlencode(),) page_num_list.append(previous_num) #页码显示 for i in range(show_start,show_end): self.params['page'] = i if i==self.current_page: li_num = '<li class="active"><a href="?%s">%s</a></li>' % (self.params.urlencode(), i,) else: li_num='<li><a href="?%s">%s</a></li>'%(self.params.urlencode(),i,) page_num_list.append(li_num) # 下一页 if self.current_page == self.get_page_num: next_num = '<li class="disabled"><a >下一页</a></li>' else: self.params['page'] = self.current_page+1 next_num = '<li><a href="?%s">下一页</a></li>' % (self.params.urlencode(),) page_num_list.append(next_num) # 尾页 self.params['page'] = self.get_page_num last_page = '<li><a href="?%s">尾页</a></li></ul></nav>' % (self.params.urlencode(),) page_num_list.append(last_page) return "".join(page_num_list)
调用方式
from django.shortcuts import render from app01.models import Book from page import Paginator def index(request): #批量创建数据 # book_list=[] # for i in range(1,101): # book=Book(title="书籍%s"%(i,),price=random.randint(50,300)) # book_list.append(book) # Book.objects.bulk_create(book_list) data_list=Book.objects.all() current_page=request.GET.get('page') paginator=Paginator(request,total_count=data_list.count(),current_page=current_page) data_list=data_list[paginator.start:paginator.end] return render(request,'index.html',{"data_list":data_list,"paginator":paginator})
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <ul> {#数据显示#} {% for data in data_list %} <li>{{ data.title }}----{{ data.price }}</li> {% endfor %} </ul> {#页码显示#} {{ paginator.html|safe }} </body> </html>