我在我的站点中有一个分页,但是它显示的每一页都是1-19页,我只想显示5页
我该怎么做?
视图.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)
我的分页.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>
看看https://medium.com/@sumitlni/paginate-properly-please-93e7ca776432是否有帮助。代码使用10表示“邻居”,但使用标记时可以将其更改为5。
你已经有了
{{ forloop.counter }}
在那里,你可以用它来限制到5。这是另一个解决方案,如果您感兴趣,它可能会给您更多的选择:Django Pagination Display Issue: all the page numbers show up。
最后,这里有一个非常好的教程,介绍如何加快django提供的现成分页:https://simpleisbetterthancomplex.com/tutorial/2016/08/03/how-to-paginate-with-django.html。
另一个可以使用的快速解决方案(例如(+和-5限制):
相关问题 更多 >
编程相关推荐