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 >
<div >
<div >
<div >
<div >
{% for sport in sports %}
<article >
<div >
<div >
<ul >
<li><a href="#">XAR<i ></i></a></li>
<li><a href="#">From: {{ sport.from_date }}<i ></i></a></li>
<li><a href="#">To: {{ sport.to_date }}<i ></i></a></li>
<li><a href="#">{{ sport.category }}<i ></i></a></li>
</ul>
</div>
</div>
<div >
<div >
<img src="{{ sport.featured_image.url }}" alt="">
<div >
<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 %}" >View More</a>
</div>
</div>
</div>
</article>
{% endfor %}
{% if is_paginated %}
<nav >
<ul >
{% if page_obj.has_previous %}
<li >
<a href="?page={{ page_obj.previous_page_number }}" >
<span aria-hidden="true">
<span ></span>
</span>
</a>
</li>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<li ><a href="#" >{{ num }}</a></li>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<li ><a href="?page={{ num }}" >{{ num }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li >
<a href="?page={{ page_obj.next_page_number }}" aria-label="Next">
<span aria-hidden="true">
<span ></span>
</span>
</a>
</li>
{% endif %}
</ul>
{% endif %}
</nav>
</div>
</div>
</div>
</div>
</section>
CodePudding user response:
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
CodePudding user response:
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"]