Django的分页器paginator
文件为pageDemo
models.py
from django.db import models # Create your models here.
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(decimal_places=2, max_digits=8)
views.py
from django.shortcuts import render # Create your views here.
from .models import Book
from django.core.paginator import Paginator, EmptyPage
def index(request):
'''
批量导入: #批量导入数据库文件里边
book_list = []
for i in range(100):
book = Book(title="book_%s"%i, price=i*i) #实例化一个book对象;
book_list.append(book) #实例化出100个对象;
Book.objects.bulk_create(book_list) #批量插入
return render(request, "index.html")
:param request:
:return:
'''
book_list = Book.objects.all() #拿到所有书籍数据
#分页器
paginator = Paginator(book_list, 8) #对谁分页,一页显示多少条数据 总的分页器对象
print("count:",paginator.count) #数据总数 打印:count:100
print("num_pages",paginator.num_pages) #总页数 num_pages:13
print("page_range",paginator.page_range) #页码的列表 page_range range(1,14) try: #page数字超了或者不在那个返回里就会抛出一个EmptyPage的错误
current_page_num = int(request.GET.get("page", 1)) #做成一个动态的效果。给它个默认值拿第一页,不加1就会返回一个None。当前想要的第几页的数据
current_page = paginator.page(current_page_num) #拿到第几页的page对象。
#显示某一页具体数据的两种方式:
print("object_list", current_page.object_list)
for i in current_page:
print(i)
except EmptyPage as e:
current_page=paginator.page(1) #拿到第一页的page对象
return render(request, "index.html", locals())
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<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>
<ul>
{% for book in current_page %}
<li>{{ book.title }}:{{ book.price }}</li>
{% endfor %}
</ul> <nav aria-label="Page navigation"> //分页的页面
<ul class="pagination">
{% if current_page.has_previous %} //上一页:当前页码-1 ---> href="?page={{current_page_num|add:-1}}或者下面这种写法 ;判断当前页是否还有上一页、下一页
<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 paginator.page_range %} //item为数字 这里不该写死-->page_range
{% if current_page_num == item %}
<li class="active"><a href="?page={{ item }}">{{ item }}</a></li> //也当前页码匹配成功了,加个class="active" 一点击谁颜色就会变深
{% else %}
<li><a href="?page={{ item }}">{{ item }}</a></li>
{% endif %}
{% endfor %} {% if current_page.has_next %}
<li><a href="?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>
</body>
</html>
改进版
views
from django.shortcuts import render # Create your views here.
from .models import Book
from django.core.paginator import Paginator, EmptyPage
def index(request):
'''
批量导入:
book_list = []
for i in range(100):
book = Book(title="book_%s"%i, price=i*i)
book_list.append(book)
Book.objects.bulk_create(book_list) #匹配插入
return render(request, "index.html")
:param request:
:return:
'''
book_list = Book.objects.all()
#分页器
paginator = Paginator(book_list, 3)
print("count:",paginator.count) #数据总数
print("num_pages",paginator.num_pages) #总页数
print("page_range",paginator.page_range) #页码的列表 current_page_num = int(request.GET.get("page", 1))
if paginator.num_pages>11:
if current_page_num-5 < 1: ##点1、2、3、4、5页码不动,点7开始显示左5右5
page_range = range(1, 12)
elif current_page_num+5 > paginator.num_pages: #如果大于最大页码数
page_range = range(paginator.num_pages-10, paginator.num_pages+1)
else: else:
current_page = paginator.page_range try:
current_page_num = int(request.GET.get("page", 1))
current_page = paginator.page(current_page_num)
#显示某一页具体数据的两种方式:
print("object_list", current_page.object_list)
for i in current_page:
print(i)
except EmptyPage as e:
current_page=paginator.page(1)
return render(request, "index.html", locals())
index.html
{% for item in page_range %} 不应该写死,其他同上
{% if current_page_num == item %}
<li class="active"><a href="?page={{ item }}">{{ item }}</a></li>
{% else %}
<li><a href="?page={{ item }}">{{ item }}</a></li>
{% endif %}
{% endfor %}