'Django Paginate_by not displaying proper pagination

Hello i have a page using paginate_by 10, and instead i'm getting only 9 elements per page, even tho in the element inspector i see 10 grid spaces my for cicle is just being able to fill 9 out of 10 then it goes into the next page.

Views.py

    class VideoListView(generic.ListView):
    model = Video
    template_name = 'index.html'
    context_object_name = 'video_list'
    paginate_by = 10
    
    def get_queryset(self):
        return Video.objects.order_by('-date')
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['category_list'] = Category.objects.all()
        return context

EDIT AFTER A QUESTION. here's the template logic

    <ul id="first-category" class="items-container">
    {% for video in video_list %} 
    {% if video.home ==True %}
    <li class="item">
    </li>
    {% endif %} {% endfor %}

As possible causes i did find that when i do "paginate by 1" the page starts displaying empty pages for the pages that aren't considered in the IF. which makes me think that the if statement is taking into consideration the empty videos even tho they aren't listed. How can i fix this? i do want to filter the videos that aren't meant for the home_page Thanks a lot in advance for the reply



Solution 1:[1]

The reason this happens is because you filter in the template, so after the queryset is paginated. Filtering in the template is not a good idea, for example because it will render the pagination incorrect, but it is also inefficent.

You should filter in the view, with:

class VideoListView(generic.ListView):
    model = Video
    queryset = Video.objects.filter(home=True).order_by('-date')
    template_name = 'index.html'
    context_object_name = 'video_list'
    paginate_by = 10
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['category_list'] = Category.objects.all()
        return context

and thus remove the {% if video.home == True %} … {% endif %} part.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Willem Van Onsem