微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – Django paginator页面范围,不显示所有数字

我在我的网站上有一个分页,但它显示我的每个页面,如1-19,我只想显示5页

enter image description here

我怎样才能做到这一点?

views.py

paginator = Paginator(object_list,new_number_of_list)

    page = request.GET.get('page')

    try:
        Items = paginator.page(page)
    except PageNotAnInteger:
        Items = paginator.page(1)
    except EmptyPage:
        Items = paginator.page(paginator.num_pages)

    variables = RequestContext(request,{"Items": Items,"ForItemCount": ForItemCount,"page": page,})
    return render(request,'templates/Core/resource_base.html',variables)

我的pagination.html

<div class="pagination p1">
  <span class="step-links">
    <ul>

          {% for l in  Items.paginator.page_range %}
            <li><a href="?page={{forloop.counter}}">{{forloop.counter}}</a></li>
          {% endfor %}



    </ul>
  </span>
</div>

解决方法

您已经拥有{{forloop.counter}},您可以使用它将其限制为五.

{% for l in  Items.paginator.page_range %}
        {% if forloop.counter < 5 %}
            <li><a href="?page={{forloop.counter}}">{{forloop.counter}}</a></li>
        {% endif %}
      {% endfor %}

这是另一种解决方案,如果您感兴趣,它可能会为您提供更多选项:Django Pagination Display Issue: all the page numbers show up.

最后,这里有一个非常好的教程,可以让您了解django开箱即用的分页https://simpleisbetterthancomplex.com/tutorial/2016/08/03/how-to-paginate-with-django.html.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐