from django.core.paginator import Paginator,Page #导入模块
paginator=Paginator(res,10) # 将res对象列表按每页10个对象的方式分组,得到paginator
# paginator对象有一下的属性
# per_page: 每页显示条目数量
# count: 数据总个数
# num_pages:总页数
# page_range:总页数的索引范围,如: (1,10),(1,200)
# page: page对象
current=paginator.page(current_page) # 具体得到某一页的页对象
# 对于具体某一页的页对象来说又有一下属性
# has_next 是否有下一页
# next_page_number 下一页页码
# has_previous 是否有上一页
# previous_page_number 上一页页码
# object_list 分页之后的数据列表
# number 当前页
# paginator paginator对象
自带的分页功能适合只有上一页和下一页这种类型的分页。
from django.shortcuts import render
from django.core.paginator import Paginator,Page
import templates
def index(request):
try:
current_page=request.GET.get('current_page')
current_page=int(current_page)
except Exception:
current_page=int(1)
res=models.Student.objects.all()
page=Paginator(res,10) # 对象列表 每列呈现的对象个数
current=page.page(current_page) #具体每一页的页对象 page
return render(request,'index.html',{'res':current})
{% for i in res.object_list %}
<p>{{ i.name }} {{ i.student_cls.cls_name }}</p>
{% endfor %}
{% if res.has_previous %}
<a href="/index?current_page={{ res.previous_page_number }}" style="margin-right: 20px;">上一页</a>
{% endif %}
{% if res.has_next %}
<a href="/index?current_page={{ res.next_page_number }}">下一页</a>
{% endif %}
![](https://i-blog.csdnimg.cn/blog_migrate/09880ec5a3aa3157d3d71a4277f25c0d.png)