分页器(paginator)
分页器的使用
>>> from django.core.paginator import Paginator >>> objects = ['john', 'paul', 'george', 'ringo'] >>> p = Paginator(objects, 2) >>> p.count #数据总数 4 >>> p.num_pages #总页数 2 >>> type(p.page_range) # `<type 'rangeiterator'>` in Python 2. <class 'range_iterator'> >>> p.page_range #页码的列表 range(1, 3) # =========[1,2] >>> page1 = p.page(1) #第1页的page对象 >>> page1 <Page 1 of 2> >>> page1.object_list #第1页的数据 ['john', 'paul'] >>> page2 = p.page(2) >>> page2.object_list #第2页的数据 ['george', 'ringo'] >>> page2.has_next() #是否有下一页 False >>> page2.has_previous() #是否有上一页 True >>> page2.has_other_pages() #是否有其他页 True >>> page2.next_page_number() #下一页的页码 Traceback (most recent call last): ... EmptyPage: That page contains no results >>> page2.previous_page_number() #上一页的页码 1 >>> page2.start_index() # 本页第一条记录的序数(从1开始) 3 >>> page2.end_index() # 本页最后录一条记录的序数(从1开始) 4 >>> p.page(0) #错误的页,抛出异常 Traceback (most recent call last): ... EmptyPage: That page number is less than 1 >>> p.page(3) #错误的页,抛出异常 Traceback (most recent call last): ... EmptyPage: That page contains no results
实现一个分页效果:
Template:
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'bootstrap.css' %}"> </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="/blog/?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="/blog/?page={{ num }}">{{ num }}</a></li> {% else %} <li class="item"><a href="/blog/?page={{ num }}">{{ num }}</a></li> {% endif %} {% endfor %} {% if book_list.has_next %} <li class="next"><a href="/blog/?page={{ book_list.next_page_number }}">下一页</a></li> {% else %} <li class="next disabled"><a href="#">下一页</a></li> {% endif %} </ul> </div> </body> </html>
views
from django.shortcuts import render,HttpResponse # Create your views here. from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from app01.models import * def index(request): ''' 批量导入数据: Booklist=[] for i in range(100): Booklist.append(Book(title="book"+str(i),price=30+i*i)) Book.objects.bulk_create(Booklist) ''' 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",locals())
我写的代码
html页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <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"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.js"></script> </head> <body> <div class="container"> <div class="row"> <ul> {% for i in booklist %} <li>{{ i.title }},,,,,{{ i.price }}</li> {% endfor %} </ul> </div> <div class="row"> <nav aria-label="Page navigation"> <ul class="pagination"> <li> {% if booklist.has_previous %} <a href="http://127.0.0.1:8000/?page={{ booklist.previous_page_number }}">上一页</a> {% else %} <a class="disabled" href="#">上一页</a> {% endif %} </li> {% for i in p_obj_page %} <li><a href="http://127.0.0.1:8000/?page={{ i }}">{{ i }}</a></li> {% endfor %} <li> {% if booklist.has_next %} <a href="http://127.0.0.1:8000/?page={{ booklist.next_page_number }}">下一页</a> {% else %} <a class="disabled" href="#">下一页</a> {% endif %} </li> </ul> </nav> </div> </div> <script></script> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </body> </html>
view部分
def showbook(request): booklist=Book.objects.all() p_obj=Paginator(booklist,20) booklist = p_obj.page(1) p_obj_page=p_obj.page_range#这里知道了多少页,是一个页数的列表 if request.GET: number=request.GET.get("page") try: booklist=p_obj.page(number) except EmptyPage: booklist = p_obj.page(1) except PageNotAnInteger: booklist=p_obj.page(5) return render(request, "showbook.html", locals()) ################################p_obj的对象 # print(p_obj.count)可以看到总个数 # print(p_obj.num_pages)总共能分多少页 # print(p_obj.page_range)是一个列表里面是[1,2,3,4,5] ###################################### # p_obj.page(1)这里就是第二页相关的内容 # book_list=[] # for i in range(100): # book_list.append(Book(title="book%s"%i,price=30+i*i)) # Book.objects.bulk_create(book_list) return render(request,"showbook.html",locals())