after writing the view for my pagination in django, the button works fine, meaning that they load new pagesz but the problem is that all the posts still remains in all the new pages and that is not what’s expected.
views.py
def ElementLists(request):
vectors = Vectors.objects.filter(status="published").order_by("?")
paginator = Paginator(vectors, 6)
page_number = request.GET.get('page')
vector_paginator = paginator.get_page(page_number)
elementlist.html
<li class="page-item">
{% if vector_paginator.has_previous %}
<a class="page-link" href="?page={{vector_paginator.previous_page_number}}" arialabel="Previous">
<span class="ti-arrow-left">Previous</span>
<span class="sr-only">Previous</span>
</a>
{% endif %}
</li>
<li class="page-item">
{% if vector_paginator.has_next %}
<a class="page-link" href="?page={{vector_paginator.next_page_number}}" aria-label="Next">
<span class="ti-arrow-right">Load More</span>
<span class="sr-only">Next</span>
</a>
{% endif %}
</li>
Can you share your whole view and the part of the template that renders the elements of the collection you’re paginating?
Okay thanks for your response…This are the codes you requested
def ElementLists(request):
vectors = Elements.objects.filter(status="published").order_by("?")
categories = Category.objects.all()
info = Announcements.objects.all()
# Search Function
query = request.GET.get("q")
if query:
vectors = vectors.filter(
Q(title__icontains=query)).distinct()
# Pagination Function
paginator = Paginator(vectors, 6)
page_number = request.GET.get('page')
vector_paginator = paginator.get_page(page_number)
# contexts
context = {
'info': info,
'vectors': vectors,
'categories': categories,
'vector': vectors,
'vector_paginator': vector_paginator,
}
return render(request, 'vector-lists.html', context)
vector-lists.html
<ul class="pagination mt-0">
<li class="page-item">
{% if vector_paginator.has_previous %}
<a class="page-link" href="?page={{vector_paginator.previous_page_number}}" aria-label="Previous">
<i class="bi bi-arrow-left-circle-fill"></i>
<span class="sr-only">Previous</span>
</a>
{% endif %}
</li>
<!-- <li class="page-item"><a class="page-link" href="#">1</a></li> -->
<li class="page-item">
{% if vector_paginator.has_next %}
<a class="page-link" href="?page={{vector_paginator.next_page_number}}" aria-label="Next">
<i class="bi bi-arrow-right-circle-fill"></i>
<!-- <span class="sr-only">Next</span> -->
</a>
{% endif %}
</li>
</ul>
You forgot the part of the template that renders the elements of the page.
okay that’s it
{% for vector in vectors %}
<!-- Single Image Product -->
<div class="col-lg-4 col-md-6 col-sm-12">
<div class="urip_column_three">
<div class="new_vid_po09i _ol3450po" style="background: linear-gradient(230deg, rgb(140, 0, 255) 0%, rgb(255, 1, 140) 100% ); "><span>{{vector.package_category}}</span></div>
<div class="item_image_urip">
<a href="{{vector.get_absolute_url}}" class="item-img">
<img src="{{vector.image.url}}" style="height: 264px; width: 453px; object-fit: fill;" class="img-fluid" alt="" />
</a>
<div class="image_urip_caption">
<div class="urip_caption_flex">
<div class="urip_author">
<div class="urip_avater">
<a href="author-detail.html.html" class="author-img">
<img src="{{vector.creator_image.url}}" class="img-fluid" alt="" />
</a>
</div>
<div class="urip_avater_place">
<h3 class="urip_title"><a href="author-detail.html.html">{{vector.creator|title}}</a></h3>
<span>{{vector.title|truncatechars:20}}</span>
</div>
</div>
</div>
<div class="urip_caption_last">
<div class="item_list_links">
<a href="premium-stock-detail.html" class="urip_link"><i class="bi bi-download"></i></a>
<a href="premium-stock-detail.html" class="urip_link"><i class="bi bi-plus-circle-fill"></i></i></a>
<a href="premium-stock-detail.html" class="urip_link"><i style="color: red;" class="bi bi-suit-heart-fill"></i></a>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
You’re showing the elements from the vectors
QuerySet
. You need to be iterating over the pagination instance. Take a look at the for loop in the template in the docs and how it differs from what you’re using.
1 Like
I have tried a lot to make this work, please show me some code and that’s would be appreciated
Rather than:
It should be:
{% for vector in vector_paginator %}
Thank you very much, it’s working now…You really helped me out