首页 > 代码库 > Django分页

Django分页

html页面:

<div id="pagination">
    <ul id="pagination-flickr">
    {% if article_list.has_previous %}
    <li class="previous"><a href="?page={{ article_list.previous_page_number }}">&laquo;上一页</a></li>
    {% else %}
    <li class="previous-off">&laquo;上一页</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 }}">下一页 &raquo;</a></li>
    {% else %}
      <li class="next-off">下一页 &raquo;</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

 

Django分页