html页面:
<div id="pagination"> <ul id="pagination-flickr"> {% if article_list.has_previous %} <li class="previous"><a href="?page={{ article_list.previous_page_number }}">«上一页</a></li> {% else %} <li class="previous-off">«上一页</li> {% endif %} <li class="active">{{ article_list.number }}/{{ article_list.paginator.num_pages }}</li> {% if article_list.has_next %} <li class="next"><a href="?page={{ article_list.next_page_number }}">下一页 »</a></li> {% else %} <li class="next-off">下一页 »</li> {% endif %} </ul> </div>
view.py:
def otherFunction(request): .... article_list = Article.objects.filter(XXX=xxx) getPage(article_list) .... def getPage(request, article_list): paginator = Paginator(article_list, 2) #用Paginator包装一下article_list,并设定一页2条 try: page = int(request.GET.get(‘page‘, 1)) article_list = paginator.page(page) #根据页码取数据 except (EmptyPage, InvalidPage, PageNotAnInteger): article_list = paginator.page(1) return article_list
时间: 2024-09-30 21:11:31