Ad

Pagination Not Taking Effect On Webpage

I'm applying pagination on my sports.html page. In views.py I'm using ListView and using paginate_by = 2 but issue is pagination is not taking effect on webpage. Pagination numbers are visible on page and when clicking on those page numbers it's not showing any error but all posts are visible on all pages, posts are not divided by paginate_by value. Can anyone point out what I'm doing wrong here ?

views.py

class SportsList(ListView):
    model = Sports
    template_name = 'frontend/sports.html'
    paginate_by = 2

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['main'] = Main.objects.all()
        context['sports'] = Sports.objects.all()
        return context
 

sports.html

<section class="blog_area">
        <div class="container">
            <div class="row">
                <div class="col-lg-8">
                    <div class="blog_left_sidebar">
                        {% for sport in sports %}
                        <article class="row blog_item">
                            <div class="col-md-3">
                                <div class="blog_info text-right">
                                    <ul class="blog_meta list">
                                        <li><a href="#">XAR<i class="lnr lnr-user"></i></a></li>
                                        <li><a href="#">From: {{ sport.from_date }}<i class="lnr lnr-calendar-full"></i></a></li>
                                        <li><a href="#">To: {{ sport.to_date }}<i class="lnr lnr-calendar-full"></i></a></li>
                                        <li><a href="#">{{ sport.category }}<i class="lnr lnr-layers"></i></a></li>
                                    </ul>
                                </div>
                            </div>
                            <div class="col-md-9">
                                <div class="blog_post">
                                    <img src="{{ sport.featured_image.url }}" alt="">
                                    <div class="blog_details">
                                        <a href="{% url 'sports_details' sport.slug %}">
                                            <h2>{{ sport.title }}</h2>
                                        </a>
                                        <p>{{ sport.content|truncatewords:30|safe }}</p>
                                        <a href="{% url 'sports_details' sport.slug %}" class="blog_btn">View More</a>
                                    </div>
                                </div>
                            </div>
                        </article>
                        {% endfor %}
                        {% if is_paginated %}
                        <nav class="blog-pagination justify-content-center d-flex">
                            <ul class="pagination">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a href="?page={{ page_obj.previous_page_number }}" class="page-link">
                                        <span aria-hidden="true">
                                            <span class="lnr lnr-chevron-left"></span>
                                        </span>
                                    </a>
                                </li>
                            {% endif %}
                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li class="page-item active"><a href="#" class="page-link">{{ num }}</a></li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li class="page-item"><a href="?page={{ num }}" class="page-link">{{ num }}</a></li>
                                {% endif %}
                            {% endfor %}
                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a href="?page={{ page_obj.next_page_number }}" class="page-link" aria-label="Next">
                                        <span aria-hidden="true">
                                            <span class="lnr lnr-chevron-right"></span>
                                        </span>
                                    </a>
 
                                </li>
                            {% endif %}
                            </ul>
                            {% endif %}
                        </nav>
                    </div>
                </div>
           </div>
              </div>
  </section>
Ad

Answer

This is because you are creating your queryset on get_context_data. You are not respecting the listview structure. Try something like this on your view:

class SportsList(ListView):
    queryset = Sport.objects.all()
    context_object_name = “sports”
    template_name = 'frontend/sports.html'
    paginate_by = 2
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['main'] = Main.objects.all()
        return context
Ad
source: stackoverflow.com
Ad