通过Queryset进行Django分页

通过Queryset进行Django分页,django,django-models,django-views,django-templates,Django,Django Models,Django Views,Django Templates,我正在基于过滤的查询实现分页,它在单击页码之前工作正常,但在单击页码之后,它会显示所有对象,包括未过滤的对象。 升级到django的最新版本后开始发生 单击前分页 单击第2页后分页 fitered objects为4,并且为每页分页器指定了2个项目,分页应在第2页处停止,但它显示了一个包含所有对象的新分页器,即使是未过滤的对象。 下面是一些片段。任何帮助都将不胜感激 视图.py Template.html {%if querySet.has_other_pages%} {%如果query

我正在基于过滤的
查询实现
分页
,它在单击页码之前工作正常,但在单击页码之后,它会显示所有对象,包括未过滤的对象。 升级到django的最新版本后开始发生

单击前分页

单击第2页后分页

fitered objects
4
,并且为每页分页器指定了
2个项目
,分页应在
第2页
处停止,但它显示了一个包含所有对象的新分页器,即使是未过滤的对象。 下面是一些片段。任何帮助都将不胜感激

视图.py

Template.html


{%if querySet.has_other_pages%}
    {%如果querySet.has_previous%}
  • {%else%}
  • {%endif%} {%endfor%} {%如果querySet.has_next%}
  • {%else%}
  • {%endif%}
{%endif%}
找到了此问题的解决方案


{%如果paginate.has_其他_页面%}
    {%如果paginate.has_previous%}
  • {%else%}
  • {%endif%} {%endfor%} {%如果paginate.has_next%}
  • {%else%}
  • {%endif%}
{%endif%}
def searchPropertyListView(request):
 city = City.objects.all().annotate(
     num_property=Count("property")).order_by("-num_property")
 categories = Category.objects.all()
 purposes = Purpose.objects.all()

 featured = list(Property.objects.filter(featured=True))
 shuffle(featured)

 querySet = Property.objects.all()
 city_or_neighborhood = request.GET.get('city_or_neighborhood')
 category = request.GET.get('category')
 purpose = request.GET.get('purpose')

 if city_or_neighborhood != '' and city_or_neighborhood is not None:
     querySet = querySet.filter(Q(city__title__icontains=city_or_neighborhood)
                               | Q(neighborhood__title__icontains=city_or_neighborhood)
                               ).distinct()
 elif category != '' and category is not None:
     querySet = querySet.filter(category__title=category)

 elif purpose != '' and purpose is not None:
     querySet = querySet.filter(purpose__title=purpose)

 paginator = Paginator(querySet, 2)

 page = request.GET.get('page')

 try:
     querySet = paginator.get_page(page)
 except PageNotAnInteger:
     querySet = paginator.get_page(1)
 except EmptyPage:
     querySet = paginator.get_page(paginator.num_pages)

 context = {
     'city': city,
     'featured': featured,
     'querySet': querySet,
     'categories': categories,
     'purposes': purposes,
 }

 return render(request, 'search/search_list.html', context)
            <nav class="mt-5">
               {% if querySet.has_other_pages %}
                <ul class="pagination justify-content-center">
                   {% if querySet.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{querySet.previous_page_number}}" tabindex="-1"><i class="mdi mdi-chevron-left"></i></a>
                    </li>
                   {% else %}
                    <li class="page-item disabled">
                        <a class="page-link" tabindex="-1"><i class="mdi mdi-chevron-left"></i></a>
                    </li>
                   {% endif %}
            
                   {% for pages in querySet.paginator.page_range %}
                    {% if querySet.number == pages %}
                     <li class="page-item active"><a class="page-link" style="height: 100% !important">{{pages}}</a></li>
                    {% else %}
                     <li class="page-item"><a class="page-link " style="height: 100% !important" href="?page={{pages}}">{{pages}}</a></li>
                    {% endif %}
                   {% endfor %}
            
                   {% if querySet.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ querySet.next_page_number }}"><i class="mdi mdi-chevron-right"></i></a>
                    </li>
                   {% else %}
                    <li class="page-item disabled">
                        <a class="page-link" href="#"><i class="mdi mdi-chevron-right"></i></a>
                    </li>
                   {% endif %}
                </ul>
                {% endif %}
            </nav>
<nav class="mt-5">
{% if paginate.has_other_pages %}
<ul class="pagination justify-content-center">
    {% if paginate.has_previous %}
    <li class="page-item">
        <a class="page-link" href="{{ request.get_full_path }}&page={{paginate.previous_page_number}}" tabindex="-1"><i class="mdi mdi-chevron-left"></i></a>
    </li>

    {% else %}
    <li class="page-item disabled">
        <a class="page-link" tabindex="-1"><i class="mdi mdi-chevron-left"></i></a>
    </li>
    {% endif %}

     {% for pages in paginate.paginator.page_range %}
      {% if paginate.number == pages %}
       <li class="page-item active"><a class="page-link" style="height: 100% !important">{{pages}}</a></li>
      {% else %}
       <li class="page-item"><a class="page-link " style="height: 100% !important" href="{{ request.get_full_path }}&page={{pages}}">{{pages}}</a></li>
      {% endif %}
     {% endfor %}

    {% if paginate.has_next %}
    <li class="page-item">
        <a class="page-link" href="{{ request.get_full_path }}&page={{ paginate.next_page_number }} ">
        <i class="mdi mdi-chevron-right"></i></a>
    </li>
    {% else %}

    <li class="page-item disabled">
        <a class="page-link" href="#"><i class="mdi mdi-chevron-right"></i></a>
    </li>
    {% endif %}
</ul>
{% endif %}