本文介绍了使用Bootstrap进行Django分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我即将在我的联系人列表中添加分页。我整天坐在那里,不知道我混了什么。重要的是我确实有一个有效的过滤器-因此我可以缩小列表的范围。但是据我了解,分页应该仍然有效。就我而言,我什么都看不到,所以我的猜测是第一个如果失败。
如果您能指出正确的方向。
最好的问候。
I am about to add a pagination to my list of contacts. I am sitting over it the whole day and have no idea what I mixed up. Important thing is that I do have a working filters - so I can narrow the list. But from my understanding pagination should work anyway. In my case I see nothing so my guess is first "if" fails. If you could point me in the right direction. Best regards.
Views.py
def ContactsList(request):
contacts_list = Contact.objects.all()
Contacts_filter = LFilter(request.GET, queryset=contacts_list)
#pagination
page = request.GET.get('page', 1)
paginator = Paginator(contacts_list, 20)
try:
contacts = paginator.page(page)
except PageNotAnInteger:
contacts = paginator.page(1)
except EmptyPage:
contacts = paginator.page(paginator.num_pages)
return render(request, 'index.html', context, {'filter': contacts_filter})
模板部分:
{% if contacts.has_other_pages %}
<ul class="pagination">
{% if contacts.has_previous %}
<li><a href="?page={{ contacts.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in contacts.paginator.page_range %}
{% if library.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if contacts.has_next %}
<li><a href="?page={{ contacts.next_page_number }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
推荐答案
问题这行:
return render(request, 'index.html', context, {'filter': contacts_filter})
在这里,您仅通过 filter
传递上下文,并且缺少个联系人
here you are passing the context as filter
only and missing contacts
因此将其更改为
return render(request, 'index.html', context, {'filter': contacts_filter,'contacts': contacts})
这篇关于使用Bootstrap进行Django分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!