Django组件之分页器
Posted sxy-blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django组件之分页器相关的知识,希望对你有一定的参考价值。
Django组件之分页器
分页器的使用
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 ‘‘‘
实现示例
views.py from django.shortcuts import render,HttpResponse # Create your views here. from app01.models import Book from django.core.paginator import Paginator,EmptyPage def index(request): """ 创建数据 book_list=[] for i in range(100): book_obj=Book(title=‘book_%s‘%i, price=i*i) book_list.append(book_obj) Book.objects.bulk_create(book_list) """ book_list = Book.objects.all() paginator = Paginator(book_list, 3) # 一页显示多少条数据 try: print(paginator.count) # 数据总数 print(paginator.num_pages) # 总页数 print(paginator.page_range) # 页数range current_page_num = int(request.GET.get(‘page‘)) # 获取当前的页数 current_page = paginator.page(current_page_num) # 得到当前页数的页数对象 # 对分页码进行分类讨论
# 处理页码头 和 页码尾部的问题
if current_page_num-5 <1 : page_range = range(1,12) elif current_page_num+6 > paginator.num_pages: page_range=range(paginator.num_pages-11,paginator.num_pages+1) else: page_range = range(current_page_num-5,current_page_num+6) # 获取第一页数据的两种方法 # print(current_page.object_list) # for i in current_page: # print(i) except EmptyPage: current_page = paginator.page(1) return render(request,‘index.html‘,locals())
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <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> {% for book in current_page %} <p>{{ book.title }}:{{ book.price }}</p> {% endfor %} <nav aria-label="Page navigation"> <ul class="pagination"> {% if current_page.has_previous %} <li> <a href="?page={{ current_page.previous_page_number }}" aria-label="Previous"> <span aria-hidden="true">上一页</span> </a> </li> {% else %} <li class="disabled"> <a href="" aria-label="Previous"> <span aria-hidden="true">上一页</span> </a> </li> {% endif %} {% for item in page_range %} {% if item == current_page_num%} <li class="active"><a href="?page={{ item }}">{{ item }}</a></li> {% else %} <li><a href="?page={{ item }}">{{item}}</a></li> {% endif %} {% endfor %} {% if current_page.has_next %} <li> <a href="?path={{ current_page.next_page_number }}" aria-label="Next"> <span aria-hidden="true">下一页</span> </a> </li> {% else %} <li class="disabled"> <a href="" aria-label="Next"> <span aria-hidden="true">下一页</span> </a> </li> {% endif %} </ul> </nav> </body> </html>
以上是关于Django组件之分页器的主要内容,如果未能解决你的问题,请参考以下文章