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.

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...