一 、分页器的简介
在页面显示分页数据,需要用到django分页器组件
from django.core.paginator import Paginator
paginator对象:paginator = Paginator(user_list,10) #per_page:每页显示条目数量 #count :数据总个数 #num_pages:总页数 #page_range:总页数的索引范围,如:(1,10),(1,100) page: page对象 page=paginator.page(1) has_next 是否有下一页 next_page_number 下一页页码 has_previous 是否有上一页 previous_page_number 上一页页码 object_list 分页之后的数据列表 number 当前页 paginator paginator对象
二、应用view层
from django.shortcuts import render,HttpResponse # Create your views here. from app01.models import * from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# 创建100行的数据
def index(request):
l1 = []
for i in range(100):
l1.append(models.Book(name='图书%s'%i, price=10+i))
# 批量插入,两个 参数,第一个是对象 列表,第二个是一次插入到数据量,
# 不填,默认一次全插入
models.Book.objects.bulk_create(l1)
return HttpResponse('ok')
'''
# 查询所有图书
book_list=models.Book.objects.all()
# 分页器--类
# 实例化产生 一个对象
# 两个参数 :object_list:对象列表,pre_page:每页显示的条数
paginator=Paginator(book_list,10)
# 对象内的属性
# 数据总条数 100条
print(paginator.count)
# 总页数10页
print(paginator.num_pages)
# 页码数的列表
print(paginator.page_range)
# 取到第x页,返回一个Page 对象
current_page=paginator.page(5)
# 当前页码内的所有数据
print(current_page.object_list)
# 是否有下一页
print(current_page.has_next())
# 是否有上一页
print(current_page.has_previous())
# 下一页页码数
print(current_page.next_page_number())
# 上一页页码数
print(current_page.previous_page_number())
return render(request,'index.html',locals())
'''
实例 :
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.shortcuts import render, HttpResponse # Create your views here. from app01 import models from django.core.paginator import Paginator, EmptyPage # def index(request): # l1 = [] # for i in range(100): # l1.append(models.Book(name='图书%s'%i, price=10+i)) # # 批量插入,两个 参数,第一个是对象 列表,第二个是一次插入到数据量, # # 不填,默认一次全插入 # models.Book.objects.bulk_create(l1) # return HttpResponse('ok') def index_next(request): book_list = models.Book.objects.all() paginator = Paginator(book_list, 10) # 如果页码数多,让它显示前5,后5,中间是 当前 所在的页码 try: current_page_num = int(request.GET.get('page')) current_page = paginator.page(current_page_num) print(current_page.object_list) # 总页数,大于11的时候 if paginator.num_pages > 11: # 当前页码数-5大于1的时候,page_range应该是? if current_page_num - 5 < 1: page_range = range(1, 12) elif current_page_num + 5 > paginator.num_pages: # 当前页码数+5大于总页码数,总页码数 往前推11个 page_range = range(paginator.num_pages - 10, paginator.num_pages + 1) else: page_range = range(current_page_num - 5, current_page_num + 6) else: # 小于11,有多少页,就显示多少页 page_range = paginator.page_range except Exception as e: current_page_num = 1 current_page = paginator.page(current_page_num) return render(request, 'index_next.html', locals())
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- 最新版本的 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"> <title>Title</title> </head> <body> <div class="row"> <div class="col-md-6 col-md-offset-3"> <table class="table table-striped"> <thead> <tr> <th>书名</th> <th>价格</th> </tr> </thead> <tbody> {% for book in current_page %} <tr> <td>{{ book.name }}</td> <td>{{ book.price }}</td> </tr> {% endfor %} </tbody> </table> <nav aria-label="Page navigation"> <ul class="pagination"> {% if current_page.has_previous %} <li> {# <a href="/index/?page={{ current_page_num|add:-1 }}" aria-label="Previous">#} <a href="/index/?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 foo in paginator.page_range %} {% if current_page_num == foo %} {# 当前页码等于循环到的页码数,变色#} <li class="active"><a href="/index/?page={{ foo }}">{{ foo }}</a></li> {% else %} <li><a href="/index/?page={{ foo }}">{{ foo }}</a></li> {% endif %} {% endfor %} {% if current_page.has_next %} <li> {# <a href="/index/?page={{ current_page_num|add:1 }}" aria-label="Next">#} <a href="/index/?page={{ 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> </div> </div> </body> </html>