语法基础:
def index(request):
'''
批量导入,数据准备:Book.objects.bulk_create
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)
:param request:
:return:
'''
'''
book_list=Book.objects.all()
# for循环在html文件里全部展示,数据太多不能全放在一页上
{% for book in book_list %}
<li>{{ book.title }}:{{ book.price }}</li>
{% endfor %}
'''
# 分页器 先引入 Paginator
book_list = Book.objects.all()
# 获取paginator 对象
paginator = Paginator(book_list, 3) # 两个参数,(操作对象,每页显示条目数)
# paginator 对象的常用属性和方法
print(paginator.count) # 数据总数
print(paginator.num_pages) # 总页数
print(paginator.page_range) # 页码的列表范围,range顾头不顾尾
page2 = paginator.page(2)
print(page2.has_next()) # 是否有下一页
print(page2.next_page_number()) # 下一页的页码
print(page2.has_previous()) # 是否有上一页
print(page2.previous_page_number()) # 上一页的页码
page1 = paginator.page(1) # 第1页的page对象,显示某一页具体数据
# 两种方法获取指定页的所有数据
for i in page1: # 遍历第1页的所有数据对象
print(i)
print(page1.object_list) # 第1页的所有数据,queryset对象,
# get请求获取page的值,得到当前页面,如果没有默认是1
current_page_num = int(request.GET.get("page", 1))
# 捕获异常,如果浏览器端输入数据page超过范围,默认显示第一页
try:
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())
<ul>
{% for book in current_page %}
<li>{{ book.title }}:{{ book.price }}</li>
{% endfor %}
</ul>
+bootstrap分页栏:
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.objects.bulk_create
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)
:param request:
:return:
'''
# 分页器 先引入 Paginator
book_list = Book.objects.all()
paginator = Paginator(book_list, 3)
current_page_num = int(request.GET.get("page", 1))
# page_range 决定html底部分页栏标签的个数
# 页面分页栏始终显示11个,左5右5,同时处理下两边不够5时分页栏显示范围。
if paginator.num_pages > 11:
if current_page_num - 5 < 1:
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:
page_range = range(current_page_num - 5, current_page_num + 6)
else:
page_range = paginator.page_range
# 捕获异常,如果get请求中路径后?拼接的数据page的值不在范围内默认显示第一页。
try:
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())
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 %}
<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 %}
{#如果当前页和分页栏的标签值相等时,给li标签加 active,颜色变深#}
{# a标签:?+page拼成get请求的数据 #}
{% 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 %}
{% if current_page.has_next %}
<li><a href="?page={{ current_page.next_page_number }}" aria-label="Next"><span aria-hidden="true">下一页</span></a>
{% else %}
<li class="disabled"><a href="" aria-label="Next"><span aria-hidden="true">下一页</span></a>
{% endif %}
</li>
</ul>
</nav>
</body>
</html>