Django 分页器
分液器
在页面显示分页数据,需要用到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对象
视图层
def index(request):
# li=[]
# for i in range(100):
# li.append(models.Book(name='图书%s'%i,price=10+i))
#数据库批量新建数据
# models.Book.objects.bulk_create(li)
# return HttpResponse('ok')
#获取book表内的所有数据
books = models.Book.objects.all()
#Paginator( object_list, per_page) object_list是要传入一个对象列表,per_page这一页显示多少条数据
paginator = Paginator(books, 3)
try:
#取出前台传过来的页码
current_page_num = int(request.GET.get('page'))
#生成当前页的对象
current_page = paginator.page(current_page_num)
#判断总页数是不是大于11
if paginator.num_pages > 11:
#判断前台传过来的页码-5是不是小于1
if current_page_num - 5 < 1:
# 当前页码数-5小于1,总页码1,到总页码12之间
pagerange = range(1, 12)
# 判断当前页码数+5大于是否总页码数
elif current_page_num + 5 > paginator.num_pages:
# 当前页码数+5大于总页码数,总页码-10,到总页码+1之间
pagerange = range(paginator.num_pages - 10, paginator.num_pages + 1)
else:
#其他情况,左5,右6推算,也就是:range(current_page_num - 5, current_page_num + 6)
pagerange = range(current_page_num - 5, current_page_num + 6)
else:
#小于11,有多少页,就显示多少页
pagerange = paginator.page_range
except Exception as e:
pagerange = range(1, 12)
current_page_num = 1
current_page = paginator.page(current_page_num)
return render(request, 'index.html', locals())
'''
分页器的使用:
book_list=Book.objects.all()
paginator = Paginator(book_list, 10)
print("count:",paginator.count) #数据总数
print("num_pages",paginator.num_pages) #总页数
print("page_range",paginator.page_range) #页码的列表
page1=paginator.page(1) #第1页的page对象
for i in page1: #遍历第1页的所有数据对象
print(i)
print(page1.object_list) #第1页的所有数据
page2=paginator.page(2)
print(page2.has_next()) #是否有下一页
print(page2.next_page_number()) #下一页的页码
print(page2.has_previous()) #是否有上一页
print(page2.previous_page_number()) #上一页的页码
# 抛错
#page=paginator.page(12) # error:EmptyPage
#page=paginator.page("z") # error:PageNotAnInteger
'''
模板层
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> <title>Title</title> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <table class="table"> <tr> <td>图书</td> <td>价格</td> </tr> {% for book in current_page %} //根据后台传过来的当前页面的数据进行打印 <tr> <td>{{ book.name }}</td> <td>{{ book.price }}</td> </tr> {% endfor %} </table> <nav aria-label="Page navigation"> <ul class="pagination"> {% if current_page.has_previous %} //has_previous 进行判断是否有上一页 <li> <a href="/index/?page={{ current_page.previous_page_number }}" aria-label="Previous"> <span aria-hidden="true">上一页</span> </a> </li> {% else %} <li> <a href="/index/?page={{ current_page_num }}" aria-label="Previous"> <span aria-hidden="true">上一页</span> </a> </li> {% endif %} {% for foo in pagerange %} {% if current_page_num == foo %} <li class="active"><a href="/index/?page={{ foo }}">{{ foo }}</a></li> {% else %} <li><a href="/index/?page={{ foo }}">{{ foo }}</a></li> {% endif %} {% endfor %} {% if current_page.has_next %} //has_next 进行判断是否有上一页 <li> <a href="/index/?page={{ current_page.next_page_number }}" aria-label="Next"> <span aria-hidden="true">下一页</span> </a> </li> {% else %} <li> <a href="/index/?page={{ current_page }}" aria-label="Next"> <span aria-hidden="true">下一页</span> </a> </li> {% endif %} </ul> </nav> </div> </div> </div> </body> <script src="/static/jquery-3.3.1.js"></script> <script src="/static/bootstrap-3.3.7-dist/js/bootstrap.js"></script> </html>