0

I am trying to use a pagination to paginate one of my pages. I have tried a few different methods I have found online but for some reason, when I use paginate_by = 3, it doesn't actually paginate anything, yet still shows the html for the pagination at the bottom of the page.

View:

class SearchListView(ListView):
    model = Post
    template_name = "public/search.html"
    paginate_by = 3

HTML:

{% extends 'public/base.html' %}
{% load staticfiles %}

{% block head %}
<link rel="stylesheet" type="text/css" href="{% static "public/css/search.css" %}" />
{% endblock %}

{% block content%}
<div class="search container-fluid">
    <img src="/media/about-us.jpg" alt="">
    <div class="search-title">
        <h1 class="title">Search</h1>
    </div>
    <div class="search-main mb-5">
        <form method='GET' action=''>
            <input type="text" name='q' class="homebanner-search" placeholder="Enter your keywords" value='{{ request.get.q }}'>
        </form>
    </div>
</div>
<div class="container mt-5 mb-5">
    <div class="detail-container">
        {% for post in queryset %}
        <a href="{% url 'post-detail' post.slug %}">
            <div class="post-main">
                <div class="post-image">
                    <img src="{{ post.image.url }}" class="card-img-top" alt="#">
                    <p class="post-category">{{ post.category }}</p>
                </div>
                <div class="post-body">
                    <div class="post-title">
                        <p class="post-title-p">Day in the life of {{ post.title }}</p>
                    </div>
                    <div class="post-text">
                        <p class="post-author-text text-muted">{{ post.sub_description|truncatewords:22 }}</p>
                    </div>
                    <div class="post-button">
                        <p>READ MORE ></p>
                    </div>
                </div>
            </div>
        </a>
        {% endfor %}
    </div>
    <div id="page_navigation" >
        {% if is_paginated %}
        <ul class="pagination">
            {% if page_obj.has_previous %}
                <li><a href="?q={{ request.GET.q }}?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
            {% else %}
                <li class="disabled"><span>&laquo;</span></li>
            {% endif %}
            {% for i in paginator.page_range %}
                {% if page_obj.number == i %}
                    <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
                {% else %}
                    <li><a href="?q={{ request.GET.q }}?page={{ i }}">{{ i }}</a></li>
                {% endif %}
            {% endfor %}
            {% if page_obj.has_next %}
                <li><a href="?q={{ request.GET.q }}?page={{ page_obj.next_page_number }}">&raquo;</a></li>
            {% else %}
                <li class="disabled"><span>&raquo;</span></li>
            {% endif %}
        </ul>
        {% endif %}
    </div>
</div>
{% endblock %}

So on the page, 3 items should be showing, and pagination should take me to the the next set of 3. The html is showing, and when I click the links it is taking me 2 page 2. The problem is the fact that 6 items are showing, and not 3, and when I go to page 2, there are the same 6 items there.

Max Loyd
  • 408
  • 6
  • 21

1 Answers1

1

Unfortunately I couldn't reproduce your error exactly, but what did happen is that my objects wouldn't render when looping through queryset. So what I would recommend is try to loop through object_list instead:

{% for post in object_list %}
    {{ post.name }}
{% endfor %}

Another thing you can do is add a context_object_name argument to your view:

class SearchListView(ListView):
    model = Post
    template_name = "public/search.html"
    paginate_by = 3
    context_object_name = 'posts'

and then loop through that:

{% for post in posts %}
    {{ post.name }}
{% endfor %}

Also, I can't visualise what the search form is doing on this page since the ListView's model (Post) is the queryset, not whatever is searched for? So perhaps the link href is causing some trouble. Maybe try something like this instead:

<li><a href="{{ request.get_full_path }}?page={{ page_obj.previous_page_number }}">&laquo;</a></li>

Again, I could not reproduce the exact problem you are having, which is a shame because I feel like I have had the same issue before, so these are just suggestions pieced together from pagination that works for me and the code you posted. Hope it points you in the right direction.

  • Thank you, I will give this a try when I am back home. Not going to be back for a good few days now, but I will try to test this asap. – Max Loyd May 07 '19 at 14:02
  • I was having an issue where pagination buttons would show but pagination not apply. Shifting to `object_list` in the forloop took care of this perfectly. :) – Carewen Apr 10 '22 at 05:29