Django infinite pagination not working

Hi, so my infinite pagination only works when i am using regular views, but i think there is a problem with class based views as only the first 6 Items are shown and i cannot scroll down for infinite pagination.

I am using a search form to search for the items (but it returns only 6 and i cannot scroll further and clicking on MORE also gives me error.

class SearchResultsView(ListView):

    model = retailer
    paginate_by = 6
    context_object_name = "prods"
    template_name = 'testing/search_results.html'
    ordering = ['price']

    def get_queryset(self):

      query = self.request.GET.get('q')

      if query :
      	All = retailer.objects.filter(title__icontains=query) 
      	return All

      else:
      	pass

MY HTML TEMPLATE:

{% block content %}



<div class="flexbox-container infinite-container">

  {% for item in prods %}

   <div class="flexbox-item infinite-item">

    <a href="/{{item.slug}}"><img src="{{item.image.url}}"></a>
    <div class="Main-Info">
      <div class = "Shop"><b>{{item.shop}}</b></div>
      <div class = "Title">{{item.title | truncatechars:29}}</div>
      
      <a href="/{{item.slug}}" target="_blank"><div class = "Price">

      {% if item.price == 0 %}

         <b>"Click Here To Find Price" </b></div> </a>

      {% elif item.country == "USA" %}

        <b>${{item.price}}</b></div></a>

      {% else %}

        <b>£{{item.price}}</b></div> </a>

      {% endif %}


    </div>


   </div>


  {% endfor %}



</div>
        


{% if page_obj.has_next %}

<a class="infinite-more-link" href="?page={{ page_obj.next_page_number }}">More</a>

{% endif %}

    <div class="loader"></div>

  </div>
  
{% endblock content %}

Clicking on More gives me error

TypeError        at /search/

object of type 'NoneType' has no len()

SO then i removed the pagination but then all the 100 plus items load

Please Help, Thanks

Can you share the full stack trace of the error?

Also, I’m not sure the context_object_name also overrides the page instance. If you look at the paginated ListView example in the docs, you’ll see they iterate over page_obj in the template.

1 Like

hi sorry for the late response

i think i found the problem, i have to also pass the page number with the query because when i go to the second page my url becomes

http://127.0.0.1:8000/search/?page=2

so thats y it throws error, but now how can i pass the title and page number together in class view ?

i can get the variable by

page = self.request.GET.get("page")

i am a bit lost after that, please help. Thanks

Alright i figured it out, by doing the below changes

<a class="infinite-more-link" href="?page={{ prods.next_page_number }}&q={{ query|urlencode }}">More</a>