django 分页

  1. views.py中的代码如下:

from django.core.paginator import Paginator
    page=int(request.GET.get('page','1'))
    objects=Jifen.objects.all().order_by('date')
    paginator=Paginator(objects,15)
    posts=paginator.page(page)

2.html中的代码如下:

<div class="panel-default">
 <center>
  <ul class="pagination">
    <li><a href="/?page=1">&laquo;</a></li>
    {%if posts.has_previous%}
    <li><a href="/?page={{posts.previous_page_number}}">上一页</a></li>
    {%else%}
    <li class="previous disabled"><a>上一页</a></li>
    {%endif%}
    {%for i in posts.paginator.page_range %}
    <li {%ifequal posts.number i%} class="active"{%endifequal%}> <a href="{{request.get_full_path}}?page={{i}}">{{i}}</a></li>
    {%endfor%}
    {%if posts.has_next%}
    <li><a href="/?page={{posts.next_page_number}}">下一页</a></li>
    {%else%}
    <li class="previous disabled"><a>下一页</a></li>
    {%endif%}
    <li><a href="{{ request.path }}?page={{ posts.paginator.num_pages }}">&raquo;</a></li>
  </ul>
 </center>
</div>

3.结果如下:

11.png


你可能感兴趣的:(django,分页)