I'm implementing pagination
based on a filtered query
, it works okay before clicking on a page number but after clicking on a page number it shows all the objects including the ones which have not been filtered.
Started occurring after upgrading to the lastest version of django.
Pagination before clicking.
Pagination after clicking on page 2.
The fitered objects
are 4
and given 2 items per page paginator
the pagination should stop at Page number 2
but it is showing a new paginator with all the objects even the ones not filtered.
Below are some snippets. Any help would be greatly appreciated.
Views.py
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)
Template.html
<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>
question from:
https://stackoverflow.com/questions/65940400/django-pagination-by-queryset 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…