【11】Django自带的分页器
一、分页
Django的分页器(paginator)
view.py
from django.shortcuts import render,HttpResponse# Create your views here.
from app01.models import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnIntegerdef index(request):
</span><span style="color: #800000;">'''</span><span style="color: #800000;"> 批量导入数据: Booklist=[] for i in range(100): Booklist.append(Book(title="book"+str(i),price=30+i*i)) Book.objects.bulk_create(Booklist) </span><span style="color: #800000;">'''</span> <span style="color: #800000;">'''</span><span style="color: #800000;">
分页器的使用:
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 </span><span style="color: #800000;">'''</span><span style="color: #000000;"> book_list</span>=<span style="color: #000000;">Book.objects.all() paginator </span>= Paginator(book_list, 10<span style="color: #000000;">) page </span>= request.GET.get(<span style="color: #800000;">'</span><span style="color: #800000;">page</span><span style="color: #800000;">'</span>,1<span style="color: #000000;">) currentPage</span>=<span style="color: #000000;">int(page) </span><span style="color: #0000ff;">try</span><span style="color: #000000;">: </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(page) book_list </span>=<span style="color: #000000;"> paginator.page(page) </span><span style="color: #0000ff;">except</span><span style="color: #000000;"> PageNotAnInteger: book_list </span>= paginator.page(1<span style="color: #000000;">) </span><span style="color: #0000ff;">except</span><span style="color: #000000;"> EmptyPage: book_list </span>=<span style="color: #000000;"> paginator.page(paginator.num_pages) </span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">"</span><span style="color: #800000;">index.html</span><span style="color: #800000;">"</span>,{<span style="color: #800000;">"</span><span style="color: #800000;">book_list</span><span style="color: #800000;">"</span>:book_list,<span style="color: #800000;">"</span><span style="color: #800000;">paginator</span><span style="color: #800000;">"</span>:paginator,<span style="color: #800000;">"</span><span style="color: #800000;">currentPage</span><span style="color: #800000;">"</span>:currentPage})</pre>
view.py
def chakanbook(request): ''' 批量导入 Booklist = [] for i in range(100):Booklist.append(models.Book(title="book" + str(i), price=20 + i * i)) models.Book.objects.bulk_create(Booklist) :param request: :return: </span><span style="color: #800000;">'''</span><span style="color: #000000;"> book_list </span>= models.Book.objects.all()<span style="color: #008000;">#</span><span style="color: #008000;"> book_list打印的是一个对象 先查看所有的书</span> paginator=Paginator(book_list,5) <span style="color: #008000;">#</span><span style="color: #008000;">这里的book_list必须是一个集合对象,把所有的书分页,一页有五个</span> <span style="color: #0000ff;">print</span>(paginator.page_range) <span style="color: #008000;">#</span><span style="color: #008000;">range(1, 4)</span><span style="color: #000000;"> num </span>= request.GET.get(<span style="color: #800000;">"</span><span style="color: #800000;">page</span><span style="color: #800000;">"</span>,2)<span style="color: #008000;">#</span><span style="color: #008000;">得到页数范围,默认有1页</span> <span style="color: #0000ff;">print</span><span style="color: #000000;">(num,type(num)) book_list </span>= paginator.page(num) <span style="color: #008000;">#</span><span style="color: #008000;">显示第一页的内容</span> <span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">"</span><span style="color: #800000;">chakan.html</span><span style="color: #800000;">"</span>,{<span style="color: #800000;">"</span><span style="color: #800000;">book_list</span><span style="color: #800000;">"</span>:book_list,<span style="color: #800000;">"</span><span style="color: #800000;">page_range</span><span style="color: #800000;">"</span>:page_range,<span style="color: #800000;">"</span><span style="color: #800000;">num</span><span style="color: #800000;">"</span>:int(num),"paginator":paginator})</pre>
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</span>=<span style="color: #000000;">Book.objects.all() paginator </span>= Paginator(book_list, 15<span style="color: #000000;">) page </span>= request.GET.get(<span style="color: #800000;">'</span><span style="color: #800000;">page</span><span style="color: #800000;">'</span>,1<span style="color: #000000;">) currentPage</span>=<span style="color: #000000;">int(page) </span><span style="color: #008000;">#</span><span style="color: #008000;"> 如果页数十分多时,换另外一种显示方式</span> <span style="color: #0000ff;">if</span> paginator.num_pages>30<span style="color: #000000;">: </span><span style="color: #0000ff;">if</span> currentPage-5<1<span style="color: #000000;">: pageRange</span>=range(1,11<span style="color: #000000;">) </span><span style="color: #0000ff;">elif</span> currentPage+5><span style="color: #000000;">paginator.num_pages: pageRange</span>=range(currentPage-5,paginator.num_pages+1<span style="color: #000000;">) </span><span style="color: #0000ff;">else</span><span style="color: #000000;">: pageRange</span>=range(currentPage-5,currentPage+5<span style="color: #000000;">) </span><span style="color: #0000ff;">else</span><span style="color: #000000;">: pageRange</span>=<span style="color: #000000;">paginator.page_range </span><span style="color: #0000ff;">try</span><span style="color: #000000;">: </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(page) book_list </span>=<span style="color: #000000;"> paginator.page(page) </span><span style="color: #0000ff;">except</span><span style="color: #000000;"> PageNotAnInteger: book_list </span>= paginator.page(1<span style="color: #000000;">) </span><span style="color: #0000ff;">except</span><span style="color: #000000;"> EmptyPage: book_list </span>=<span style="color: #000000;"> paginator.page(paginator.num_pages) </span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">"</span><span style="color: #800000;">index.html</span><span style="color: #800000;">"</span>,locals())</pre>