Django自带分页方法
# Django分页
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
def index1(request):
# 获取当前页码数
num = request.GET.get('num', 1)
n = int(num)
# 查询所有数据
reservoirs = models.ReservoirData.objects.all()
# 创建分页器对象
pager = Paginator(reservoirs, 10) # Paginator(所有数据(页码总页), 每页展示记录数)
# 获取当前页的数据
try:
perpage_date = pager.page(n)
except PageNotAnInteger:
perpage_date = pager.page(1) # 返回第一页的数据
except EmptyPage:
perpage_date = pager.page(pager.num_pages) # 返回最后一页的数据
# 每页开始页码
begin = (n - int(math.ceil(10.0/2)))
if begin < 1:
begin = 1
# 每页结束页码
end = begin + 9
if end > pager.num_pages:
end = pager.num_pages
if end <= 10:
begin = 1
else:
begin = end - 9
pagelist = range(begin, end+1)
return render(request, 'reservoir.html', {'pager': pager, 'perpage_date': perpage_date, 'pagelist': pagelist})