Django组件——分页器(paginator)
一、视图层
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.objects.create(title="book_%s" % i, price=i*i)
book_list.append(book)
Book.objects.bulk_create(book_list)
"""
# 分页器的使用:
book_list = Book.objects.all()
paginator = Paginator(book_list, 8) # 第二个参数设置每页显示的条数
print(paginator.count) # 数据总条数 100
print(paginator.num_pages) # 总页数 13
print(paginator.page_range) # 页码的列表 range(1, 14)
page1 = paginator.page(1) # 第一页的page对象
# 显示某一页具体数据的两种方式:
# 方式一:
print("object_list", page1.object_list) # object_list <QuerySet [<Book: Book object (1)>, <Book: Book object (2)>,...
# 方式二:
for i in page1:
print(i)
"""输出
Book object (1)
Book object (2)
Book object (3)
Book object (4)
Book object (5)
Book object (6)
Book object (7)
Book object (8)
"""
page2 = paginator.page(2)
print(page2.has_next()) # 是否有下一页
print(page2.next_page_number()) # 下一页的页码
print(page2.has_previous()) # 是否有上一页
print(page2.previous_page_number()) # 上一页的页码
current_page_num = int(request.GET.get("page", 1))
if paginator.num_pages > 11:
if current_page_num-5 < 1:
# 当前页码-5小于1时,默认显示前11个页码
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:
# 显示当前页码和左五右五共11个页码
page_range = range(current_page_num-5, current_page_num+6)
else:
# 不超过11个页码的情况,显示所有页码
page_range = paginator.page_range
# 超出页码范围或小于1的页码,会抛出emptypage错误,做如下处理:
try:
current_page_num = int(request.GET.get("page", 1)) # /?page=1 get取不到默认给它默认值: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) # 固定在出现emptypage时显示首页
return render(request, "index.html", locals())
注意:
1、批量生成数据,添加到数组中,再一次性写入数据库表中:Book.objects.bulk_create(book_list)
2、分页器对象生成后,.count查看数据总条数,.num_pages显示总页数,.page_range页码列表。
3、显示某一页的具体数据有两种方式:
# 方式一:
print("object_list", page1.object_list) # object_list <QuerySet [<Book: Book object (1)>, <Book: Book object (2)>,...
# 方式二:
for i in page1:
print(i)
"""输出
Book object (1)
Book object (2)
Book object (3)
Book object (4)
Book object (5)
Book object (6)
Book object (7)
Book object (8)
"""
4、.page()方法可以生成页对象,具备如下方法可用于上一页下一页网页查看。
page2 = paginator.page(2)
print(page2.has_next()) # 是否有下一页
print(page2.next_page_number()) # 下一页的页码
print(page2.has_previous()) # 是否有上一页
print(page2.previous_page_number()) # 上一页的页码
5、得到当前页码,且设置默认页码为1;根据当前页码得到当前页对象。
current_page_num = int(request.GET.get("page", 1)) # /?page=1 get取不到默认给它默认值:1
current_page = paginator.page(current_page_num)
6、针对超出页码范围的页码,会抛出emptypage错误,可以做如下处理:
# 超出页码范围或小于1的页码,会抛出emptypage错误,做如下处理:
try:
current_page_num = int(request.GET.get("page", 1)) # /?page=1 get取不到默认给它默认值: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) # 固定在出现emptypage时显示首页
7、页面具备翻页功能,设置同时最多只能显示11条,处理如下:
current_page_num = int(request.GET.get("page", 1))
if paginator.num_pages > 11:
if current_page_num-5 < 1:
# 当前页码-5小于1时,默认显示前11个页码
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:
# 显示当前页码和左五右五共11个页码
page_range = range(current_page_num-5, current_page_num+6)
else:
# 不超过11个页码的情况,显示所有页码
page_range = paginator.page_range
二、模板层
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.css">
</head>
<body>
<ul>
{% for book in current_page %}
<li>{{ book.title }}:{{ book.price }}</li>
{% endfor %}
</ul>
<nav aria-label="Page navigation">
<ul class="pagination">
{# <li>#}
{# 方法一:上一页在当前页码减1 #}
{# <a href="?page={{ current_page_num|add:-1 }}" aria-label="Previous">#}
{# <span aria-hidden="true">上一页</span>#}
{# </a>#}
{# </li>#}
{# 方法二:#}
{% if current_page.has_previous %}
<li><a href="?page={{ current_page_num.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>
{% else %}
{# 页码到第一页后,上一页设置为禁用 #}
<li class="disabled"><a href="?page={{ current_page_num.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>
{% endif %}
{# 自动生成<li>标签,路径前面不变得情况下从?开始配置即可 #}
{% for item in page_range %}
{# 当前页面添加active显示高亮 #}
{% 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 %}
{% if current_page.has_next %}
<li><a href="?page={{ current_page_num.next_page_number }}" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>
{% else %}
<li class="disabled"><a href="?page={{ current_page_num.next_page_number }}" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>
{% endif %}
</ul>
</nav>
</body>
</html>
注意:
1、读取当前页码数据生成标签展示在页面上:
<ul>
{% for book in current_page %}
<li>{{ book.title }}:{{ book.price }}</li>
{% endfor %}
</ul>
2、引入bootstrap
<head>
<link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.css">
</head>
body部分:
<nav aria-label="Page navigation">
<ul class="pagination">
...
</ul>
</nav>
3、生成页面页码标签,路径前面不变的情况下可以从?开始配置:
{# 自动生成<li>标签,路径前面不变得情况下从?开始配置即可 #}
{% for item in page_range %}
{# 当前页面添加active显示高亮 #}
{% 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 %}
4、生成页面翻页,上一页、下一页:
(1)第一种方式是在当前页码做加减法:{{ current_page_num|add:-1 }};{{ current_page_num|add:1 }}
<li>
<!--方法一:上一页在当前页码减1 -->
<a href="?page={{ current_page_num|add:-1 }}" aria-label="Previous">
<span aria-hidden="true">上一页</span>
</a>
</li>
(2)第二种方式是使用page对象自带的方法:
<!--上一页-->
{% if current_page.has_previous %}
<li><a href="?page={{ current_page_num.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>
{% else %}
{# 页码到第一页后,上一页设置为禁用 #}
<li class="disabled"><a href="?page={{ current_page_num.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>
{% endif %}
<!--下一页-->
{% if current_page.has_next %}
<li><a href="?page={{ current_page_num.next_page_number }}" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>
{% else %}
<li class="disabled"><a href="?page={{ current_page_num.next_page_number }}" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>
{% endif %}