Search code examples
pythondjangopaginationdjango-pagination

Pagination in django not pagination the posts by 6 posts per page


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>

Solution

  • I later got a fix for it when looping thorught your posts in template, it should now be this

    before

    {% for post in posts %}
        ...code here
    {% endfor %}
    

    Now

    {% for post in posts_paginator %}
        ...code here
    {% endfor %}