分页器
简单的分页器实现
views.py
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
from app1.models import *
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)
book_list = Book.objects.all()
paginatior = Paginator(book_list,10)
# book_list中的数量
print(paginatior.count)
# 总页数
print(paginatior.num_pages)
# 页码的列表
print(paginatior.page_range)
# 假设get请求的数据大于或者小于了页码,会报错(EmptyPage错误),在这里捕获这个错误,然后把页码设置为首页
try:
page = paginatior.page(1)# 第一页的page对象
for i in page:# 第一页的所有数据
print(i)
# 根据get请求中的参数进行分页
current_page_num = int(request.GET.get('page',1))
current_page = paginatior.page(current_page_num)
print(current_page.has_next()) # 是否有下一页
print(current_page.next_page_number()) # 下一页的页码
print(current_page.has_previous()) # 是否有上一页
print(current_page.previous_page_number()) # 上一页的页码
except EmptyPage as e:
current_page = paginatior.page(1)
return render(request,'index.html',locals())
分页器实现页码,并且点击页码可以进行跳转
index.html
<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 %}
{% for itme in paginatior.page_range %}
{% if current_page_num == itme %}
<li class="active"><a href="?page={{ itme }}">{{ itme }}</a></li>
{% else %}
<li><a href="?page={{ itme }}">{{ itme }}</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>
优化后的分页器
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)
book_list = Book.objects.all()
paginatior = Paginator(book_list,3)
# book_list中的数量
print(paginatior.count)
# 总页数
print(paginatior.num_pages)
# 页码的列表
print(paginatior.page_range)
# 分页器优化
current_page_num = int(request.GET.get('page', 1))
if paginatior.num_pages > 11:
if current_page_num -5<1:
page_range = range(1,11)
elif current_page_num +5 > paginatior.num_pages:
page_range = range(paginatior.num_pages-10,paginatior.num_pages+1)
else:
page_range = range(current_page_num-5,current_page_num+5)
else:
page_range = paginatior.page_range
print('page',page_range)
# 假设get请求的数据大于或者小于了页码,会报错(EmptyPage错误),在这里捕获这个错误,然后把页码设置为首页
try:
page = paginatior.page(1)# 第一页的page对象
for i in page:# 第一页的所有数据
print(i)
# 根据get请求中的参数进行分页
current_page_num = int(request.GET.get('page',1))
current_page = paginatior.page(current_page_num)
print(current_page.has_next()) # 是否有下一页
print(current_page.next_page_number()) # 下一页的页码
print(current_page.has_previous()) # 是否有上一页
print(current_page.previous_page_number()) # 上一页的页码
except EmptyPage as e:
current_page = paginatior.page(1)
return render(request,'index.html',locals())