Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

Django Pagination by Queryset

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.

enter image description here Pagination before clicking.

enter image description here 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found solution on this link.

<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 %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...