加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:11:43 所属栏目:Python 来源:网络整理
导读:我在我的网站上有一个分页,但它显示我的每个页面,如1-19,我只想显示5页 我怎样才能做到这一点? views.py paginator = Paginator(object_list,new_number_of_list) page = request.GET.get('page') try: Items = paginator.page(page) except PageNotAnInteg
我在我的网站上有一个分页,但它显示我的每个页面,如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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读