带有引导程序的 Django 分页 [英] Django pagination with bootstrap

查看:28
本文介绍了带有引导程序的 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.

视图.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 }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><span>&laquo;</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 }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><span>&raquo;</span></li>
    {% endif %}
  </ul>
{% endif %}

推荐答案

此行的问题:

return render(request, 'index.html', context, {'filter': contacts_filter})

在这里,您仅将上下文作为 filter 传递,而缺少 contacts

here you are passing the context as filter only and missing contacts

所以把它改成

return render(request, 'index.html', context, {'filter': contacts_filter,'contacts': contacts})

这篇关于带有引导程序的 Django 分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆