不显示所有数字的 Django 分页器页面范围
Django paginator page range for not displaying all numbers
我的网站有分页,但它显示的每一页都是 1-19,我只想显示 5 页。
我该怎么做?
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。
看看 https://medium.com/@sumitlni/paginate-properly-please-93e7ca776432 是否有帮助。该代码使用 10 作为 "neighbors",但您可以在使用标签时将其更改为 5。
另一个可以使用的快速解决方案(例如+和- 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 %}
我的网站有分页,但它显示的每一页都是 1-19,我只想显示 5 页。
我该怎么做?
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。
看看 https://medium.com/@sumitlni/paginate-properly-please-93e7ca776432 是否有帮助。该代码使用 10 作为 "neighbors",但您可以在使用标签时将其更改为 5。
另一个可以使用的快速解决方案(例如+和- 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 %}