008:Django 分页

本章知识点

  1. 分页的介绍
  2. Django分页插件
    知识点讲解
    1、分页的介绍
    登录 --> 首页 --> 列表页
    列表页
    分页
    每页多少条
    当前页码

第一种自定义分页:

def pageDemo(request):
‘’’
自定义分页]
:param request:
:return:
‘’’
currentpage=request.GET.get(‘pageIndex’)
pageSize=2
if not currentpage or int(currentpage)<1:
currentpage=1
current_page=int(currentpage)
start=(current_page-1)pageSize
end=current_page
pageSize
data=userInfo.objects.all()[start:end]
if current_page*pageSize>userInfo.objects.all().count():
nextpage=current_page
else:
nextpage=current_page+1
if current_page<=1:
previous_page=1
else:
previous_page=current_page-1
data={
‘data’:data,
‘nextPage’:nextpage,
‘prevpage’: previous_page
}
return render(request, ‘app02/Paginator.html’, data)
html:

第二种分页:使用分页器
def pageDemoWithpaginator(request):

'''
使用django的分页器分页
:param request:
:return:
'''
#查询数据
userdata=userInfo.objects.all()
#第二步:生成分页实例
pageinstance=Paginator(userdata,2)
#获取当前页面页码
currentPage=request.GET.get('pageIndex',1)
#获取指定页码的数据
pagedata=pageinstance.page(currentPage)
#将数据返回到页面
return render(request, 'app02/Paginator.html', {'data':pagedata})

html:

猜你喜欢

转载自blog.csdn.net/weixin_43582101/article/details/86014006