16.分页django自带的分页:djangopaginator参考:https://docs.djangoproject.com/en/1.10/topics/pagination/>>>fromdjango.core.paginatorimportPaginator>>>objects=['john','paul','george','ringo']>>>p=Paginator(objects,2)>>>p.count4>>>p.num_pages##一共多少页,也可说是最后一页的页数2>>>p.page_range[1,2]>>>>>>page1=p.page(1)>>>page1<Page1of2>>>>page1.object_list['john','paul']>>>page2=p.page(2)>>>page2.object_list['george','ringo']>>>page2.has_next()False>>>page2.has_previous()True>>>page2.has_other_pages()True>>>page2.next_page_number()Traceback(mostrecentcalllast):...EmptyPage:Thatpagecontainsnoresults>>>page2.previous_page_number()1>>>page2.start_index()#The1-basedindexofthefirstitemonthispage3>>>page2.end_index()#The1-basedindexofthelastitemonthispage4>>>p.page(0)Traceback(mostrecentcalllast):...EmptyPage:Thatpagenumberislessthan1>>>p.page(3)Traceback(mostrecentcalllast):...EmptyPage:Thatpagecontainsnoresults#########################################################################################################后端处理fromdjango.core.paginatorimportPaginator,EmptyPage,PageNotAnIntegerfromdjango.shortcutsimportrenderdeflisting(request):contact_list=Contacts.objects.all()###把内容取出来,但不是真正取出去。paginator=Paginator(contact_list,25)#Show25contactsperpagepage=request.GET.get('page')##前台说要去那一页,就提交到这try:contacts=paginator.page(page)exceptPageNotAnInteger:#Ifpageisnotaninteger,deliverfirstpage.##如果页面不是一个整数,交付第一页contacts=paginator.page(1)exceptEmptyPage:#Ifpageisoutofrange(e.g.9999),deliverlastpageofresults.contacts=paginator.page(paginator.num_pages)##如果取的页数超过最大页数,就返回最后一页returnrender(request,'list.html',{'contacts':contacts})##把获取到的页面返回到前台#########################################################################################################前端处理{%forcontactincontacts%}{#Each"contact"isaContactmodelobject.#}{{contact.full_name|upper}}<br/>...{%endfor%}<divclass="pagination"><spanclass="step-links">{%ifcontacts.has_previous%}##判断后端传来的页数,有没有上一页<ahref="?page={{contacts.previous_page_number}}">previous</a>{%endif%}<spanclass="current">Page{{contacts.number}}of{{contacts.paginator.num_pages}}.</span>{%ifcontacts.has_next%}##判断后端传来的页数,有没有下一页<ahref="?page={{contacts.next_page_number}}">next</a>{%endif%}###?page,加上'?'问号。就是一个get方法。</span></div>#########################################################################################################前端处理,用bootstrap实现,作业:线上分页中的前后几页,而不是下面的全部页面都显示<nav><ulclass="pagination">{%ifarticles.has_previous%}<li><ahref="?page={{articles.previous_page_number}}"aria-label="Previous"><spanaria-hidden="true">&laquo;</span></a></li>{%else%}<li><ahref="#"aria-label="Previous"><spanaria-hidden="true">&laquo;</span></a></li>{%endif%}{%forp_numinarticles.paginator.page_range%}{%ifarticles.number==p_num%}<liclass="active"><ahref="#">{{articles.number}}<spanclass="sr-only">{{articles.number}}</span></a></li>{%else%}<li><ahref="?page={{p_num}}">{{p_num}}</a></li>{%endif%}{%endfor%}{%ifarticles.has_next%}<li><ahref="?page={{articles.next_page_number}}"aria-label="Next"><spanaria-hidden="true">&raquo;</span></a></li>{%else%}<li><ahref="#"aria-label="Next"><spanaria-hidden="true">&raquo;</span></a></li>{%endif%}</ul></nav>



下面这个实例,分页页面按钮数最多显示为3个!

实例:后端:defindex(request):##分页articles_list=models.Article.objects.all().order_by('-publish_date')###把内容取出来,但不是真正取出去。paginator=Paginator(articles_list,2)#Show2contactsperpagepage=request.GET.get('page')##前台说要去那一页,就提交到这try:articles=paginator.page(page)exceptPageNotAnInteger:#Ifpageisnotaninteger,deliverfirstpage.##如果页面不是一个整数,交付第一页articles=paginator.page(1)exceptEmptyPage:#Ifpageisoutofrange(e.g.9999),deliverlastpageofresults.articles=paginator.page(paginator.num_pages)##如果取的页数超过最大页数,就返回最后一页page_range=range(articles.number-1,articles.number+2)max_page=paginator.num_pagesreturnrender(request,'index.html',{'articles':articles,'page_range':page_range,'max_page':max_page,})前端:<divclass="pagination"><ulclass="pagination">{%ifarticles.has_previous%}<li><ahref="?page={{articles.previous_page_number}}">previous</a></li>{%endif%}{%forp_numinpage_range%}{%if0<p_numandp_num<max_page%}{%ifarticles.number==p_num%}<liclass="active"><ahref="#">{{articles.number}}<spanclass="sr-only">{{articles.number}}</span></a></li>{%else%}<li><ahref="?page={{p_num}}">{{p_num}}</a></li>{%endif%}{%endif%}{%endfor%}{%ifarticles.has_next%}<li><ahref="?page={{articles.next_page_number}}">next</a></li>{%endif%}</ul></div>

<divclass="pagination"><ulclass="pagination">{%ifarticles.has_previous%}<li><ahref="?page={{articles.previous_page_number}}">&laquo;</a></li>{%else%}<liclass="disabled"><ahref="?page={{articles.next_page_number}}">&laquo;</a></li>{%endif%}{%forp_numinpage_range%}{%if0<p_numandp_num<max_page%}{%ifarticles.number==p_num%}<liclass="active"><ahref="#">{{articles.number}}<spanclass="sr-only">{{articles.number}}</span></a></li>{%else%}<li><ahref="?page={{p_num}}">{{p_num}}</a></li>{%endif%}{%endif%}{%endfor%}{%ifarticles.has_next%}<li><ahref="?page={{articles.next_page_number}}">&raquo;</a></li>{%else%}<liclass="disabled"><ahref="?page={{articles.next_page_number}}">&raquo;</a></li>{%endif%}</ul></div>