python - 不显示所有数字的 Django 分页器页面范围

标签 python django django-pagination

我的网站有一个分页,但它显示的每一页都像 1-19,我只想显示 5 页。 enter image description here

我该怎么做?

View .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>

最佳答案

可以使用的另一个快速解决方案(例如 + 和 - 5 限制):

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

关于python - 不显示所有数字的 Django 分页器页面范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41131802/

相关文章:

django - 使用带有分页功能的通用 ListView。 Django

django - 分页过滤器

python numpy ValueError : operands could not be broadcast together with shapes

python - 如何在django中对上下文对象进行分页?

python - 管理员中的 "Caught ImportError while rendering: No module named urls"

python - 在 Django 站点中将 HTML 渲染为 PDF

Jquery 到 Python/Django 查询字典

python - 动态添加属性到 Django 模型

python - 为什么输出没有变化?

python - 我们如何有效地获取列表中所有项目在索引中的序号位置