django内置分页

一. 普通分页

参考:https://blog.csdn.net/qq_37605109/article/details/124514037
效果

1. views.py中
from django.core.paginator import Paginator

def author_list(request):
  authors = Author.objects.all()
  page = request.GET.get('page', 1)
  limit = request.GET.get('limit', 15)
  
  paginator = Paginator(authors, limit)
  page_1 = paginator.get_page(page)
  
  return render(request, 'web/show-author.html', locals())
2. show-author.html
<div id="content">
  <div  style="padding-left: 25px; ">
    <table class="table"  >
    <thead>
      <tr>
        <th>作者名称</th>
        <th>所属机构</th>
        <th>文章数</th>
      </tr>
    </thead>
    <tbody>
      {% for row in page_1 %}
        <tr>
          <td>{{ row.username }}</td>        
          <td>{{ row.org }}</td>
          <td>{{ row.articles_collected.count }}</td>
        </tr>
      {% endfor %}
    </tbody>
    </table>
  </div>
</div>

<!-- 普通分页 -->
{% if page_1.has_other_pages %}   
<div class="text-center mt-2 mt-sm-1 mt-md-0 mb-3 f-16">
    {% if page_1.has_previous  %}
    <a class="text-secondary" href="?page={{page_1.previous_page_number}}">上一页</a>
    {%endif%}
    <span class="mx-2">第&nbsp;{{page_1.number}}&nbsp;/&nbsp;{{ page_1.paginator.num_pages }}&nbsp;页</span>
    {%  if page_1.has_next %}
    <a class="text-success" href="?page={{page_1.next_page_number}}">下一页</a>
    {%  endif %}

</div>
{% endif %}
posted @ 2022-11-22 13:59  坚强的小蚂蚁  阅读(14)  评论(0编辑  收藏  举报