分页器
分页器功能:
在页面显示分页数据,需要用到Django分页器组件
from django.core.paginator import Paginator
Paginator对象: paginator = Paginator(user_list, 10) # per_page: 每页显示条目数量 # count: 数据总个数 # num_pages:总页数 # page_range:总页数的索引范围,如: (1,10),(1,200) # page: page对象 page对象:page=paginator.page(1) # has_next 是否有下一页 # next_page_number 下一页页码 # has_previous 是否有上一页 # previous_page_number 上一页页码 # object_list 分页之后的数据列表 # number 当前页 # paginator paginator对象
Views.py
from django.shortcuts import render, HttpResponse # Create your views here. from app01.models import * # 导入django内置的paginator模块 from django.core.paginator import Paginator, EmptyPage def page_test(request): # 生成测试数据 # book_list=[] # for i in range(100): # # Book.objects.create(name='book%s'%i,price=10+i) # book=Book(name='book%s'%i,price=10+i) # book_list.append(book) # 一次插入10条数据,第二个参数没有 则一次全部插入 # Book.objects.bulk_create(book_list,10) # Book.objects.bulk_create(book_list) # 获取当前页码 book_list = Book.objects.all() #这里只要不调用不会执行,数据库不可能吧全部的数据都加载到内存 paginator = Paginator(book_list, 3) # 一页3条数据 print(paginator.count) #总数据条数 插入了一百条所以就是100 print(paginator.num_pages) #总页码数 比如每页10条数据 则有100/10=10 页码数为10 print(paginator.page_range) #页码范围 range(1,11) try: #获取当前页码,如果不传,默认就是第一页 current_page = int(request.GET.get('page', 1)) # 生成page对象,传页码,会生成对应页码数据的对象 page = paginator.page(current_page) except Exception: current_page = 1 page = paginator.page(1) # for i in page.object_list: #对应页码的对象,比如显示第三页的10条数据 # print(i) # for i in page: #同上面一样 # # 当前页码的每条数据 # print(i) # 是否有下一页 # print(page.has_next()) # # 是否有上一页 # print(page.has_previous()) # # 下一页页码 # print(page.next_page_number()) # # 上一页页码 # print(page.previous_page_number()) # 显示前5 后5 总共11页 if paginator.num_pages > 11: if current_page - 5 < 1: page_range = range(1, 12) elif current_page + 5 > paginator.num_pages: page_range = range(paginator.num_pages - 10, paginator.num_pages + 1) else: page_range = range(current_page - 5, current_page + 6) else: page_range = paginator.page_range return render(request, 'page_test.html', locals())
模板层
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <title>Title</title> </head> <body> <ul> <!-- 显示对应页面的数据--> {% for book in page %} <li>{{ book.name }}</li> {% endfor %} </ul> <nav aria-label="Page navigation"> <ul class="pagination"> {% if page.has_previous %} <li> <!-- --> <a href="?page={{ 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 page_range %} {% if current_page == foo %} <li class="active"><a href="?page={{ foo }}">{{ foo }}</a></li> {% else %} <li><a href="?page={{ foo }}">{{ foo }}</a></li> {% endif %} {% endfor %} {% if page.has_next %} <li><a href="?page={{ 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>