user14567126
user14567126

Reputation: 323

Django Pagination Issues - Beginner

I'm making a project with Django and learning about pagination for the first time. I've read a lot of documentation so far but still can't get it to work. I don't know what I'm doing wrong. Any insight is helpful. From my understanding, this should make only 3 posts appear on the page. Instead, all of the posts appear. I want to make it so only 3 posts appear on the page at once. I don't get any errors with this. I just see all the posts on the page.

views.py

from .models import User, Post
from django.core.paginator import Paginator


def index(request):
    list_of_posts = Post.objects.all().order_by('id').reverse()
    paginator = Paginator(list_of_posts, 3)
    page_number = request.GET.get('page', 1)
    page_obj = paginator.get_page(page_number)

    return render(request, "network/index.html", {
        "list_of_posts": list_of_posts,
        "page_obj": page_obj
    })

Portion of the html for pagination:

{% extends "network/layout.html" %}
{% load static %}
{% block body %}
         <h3> Welcome. Here is your news feed: </h3>

{% for i in list_of_posts %}

<div class='card mb-3' style="max-width: 530px;" id="card-posts">

        <div class="row no-gutters">
                <div class="col-md-8">
                  <div class="card-body">
                    <h5 class="card-title"><a href="{% url 'profile' username=i.username %}">{{i.username}}</a></h5>
                    <p class="card-text">{{i.text}}</p>
                    <p class="card-text">{{i.timestamp}}</p>
                    <p class="card-text">{{i.amount_likes}}</p>
                  </div>
                </div>
              </div>
            </div>
{% endfor %}
<br><br>
{% endblock %}

<div class="container">
  <ul class="pagination justify-content-center">
      {% if page_obj.has_previous %}
          <li class="page-item"><a href="?page=1" class="page-link">&laquo; First</a></li>
          <li class="page-item"><a href="?page={{ page_obj.previous_page_number }}" 
class="page-link">Previous</a></li>
      {% else %}
      <li class="page-item disabled"><a class="page-link">&laquo; First</a></li>
      <li class="page-item disabled"><a class="page-link">Previous</a></li>
      {% endif %}
      

      {% if page_obj.number %}
      <li class="page-item"><a class="page-link">{{ page_obj.number }}</a></li>
      {% else %}
      <li class="page-item"><a class="page-link">0</a></li>
      {% endif %}

     {% if page_obj.has_next %}
  <li class="page-item"><a href="?page={{ page_obj.next_page_number }}" class="page-link">Next</a></li>
  <li class="page-item"><a href="?page={{ page_obj.paginator.num_pages }}" class="page-link">Last &raquo;</a></li>
  {% else %}
  <li class="page-item disabled"><a class="page-link">Next</a></li>
  <li class="page-item disabled"><a class="page-link">Last &raquo;</a></li>
  {% endif %}
  </ul>
</div>

Upvotes: 0

Views: 50

Answers (1)

Abdul Aziz Barkat
Abdul Aziz Barkat

Reputation: 21787

You are looping over the wrong variable list_of_posts is the queryset defined in the line:

list_of_posts = Post.objects.all().order_by('id').reverse()

But you want to loop over the paginated queryset which can be done by looping over page_obj. Hence your loop should be like:

{% for i in page_obj %}
...
{% endfor %}

Upvotes: 1

Related Questions