django分页的两种方式

第一种自定义分页:

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:
<div>
<a href="/app02/pagination1/?pageIndex={{nextPage}}">下一页</a>
<a href="/app02/pagination1/?pageIndex={{prevpage}}">上一页</a>
</div>

第二种分页:使用分页器
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:
<div id="pagination">
<ul id="pagination-flickr">
{% if data.has_previous %}
<li ><a href="/app02/pagination/?pageIndex={{ data.previous_page_number }}">&laquo;上一页</a></li>
{% else %}
<li >&laquo;上一页</li>
{% endif %}
<li >{{ data.number }}/{{ data.paginator.num_pages }}</li>
{% if data.has_next %}
<li ><a href="/app02/pagination/?pageIndex={{ data.next_page_number }}">下一页 &raquo;</a></li>
{% else %}
<li>下一页 &raquo;</li>
{% endif %}
</ul>
</div>


 
posted @ 2018-04-13 12:13  雪纷飞  阅读(1920)  评论(1编辑  收藏  举报