vineet singh
vineet singh

Reputation: 51

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>

Upvotes: 1

Views: 49

Answers (2)

rs_punia
rs_punia

Reputation: 449

You need to use the following in the views.py.

 context_object_name = 'sports'

By default, While a Django view is executing, self.object_list will contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon. You can see the reference in Django doc here. generic list view

Of course, you are adding 'sports' in the context of def get_context_data but a Django generic list view need to get operated upon the value mentioned in context_object_name or the object_list.

Hence in your views.py use:

class SportsList(ListView):
    model = Sports
    template_name = 'frontend/sports.html'
    paginate_by = 2
    context_object_name = 'sports'
    ordering = ["id"]

Upvotes: 1

LaCharcaSoftware
LaCharcaSoftware

Reputation: 1094

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

Upvotes: 1

Related Questions