#StackBounty: #django Filter + Paginator not working as expected

Bounty: 50

I have a filter with a dependent dropdown as well as a paginator. The paginator and filter are working as expected except if the category has a page 2 it no longer filters the results and just displays everything in the db(paginated). How do I go about carrying the filter request into the paginator.

view

model = Post

myFilter = carFilter(request.GET, queryset=Post.objects.all())
posts = myFilter.qs

page = request.GET.get('page', 1)
paginator = Paginator(posts, 2)
page_obj = paginator.get_page(page)
page_range = paginator.get_elided_page_range(number=page)

context = {
    'posts':posts, 'myFilter':myFilter, 'page_range': page_range, 'page': page, 'paginator': paginator, 'page_obj': page_obj
}

template

{% for post in page_obj %}
<article>

    <a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.year }}</a>
    <a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.manufacture }}</a>

</article>
{% endfor %}


<nav aria-label="Page navigation example " class="paginator">
    <ul class="pagination justify-content-center">
        <li class="page-item">
            {% if page_obj.has_previous %}
            <a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
            {% else %}
        </li>

        <li class="page-item disabled">
            <a class="page-link" href="#" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
        {% endif %} {% for i in page_obj.paginator.page_range %} {% if page_obj.number == i %}
        <li class="page-item active" aria-current="page">
            <a class="page-link" href="#">{{ i }}</a>
        </li>
        {% elif i > page_obj.number|add:'-3' and i < page_obj.number|add:'3' %}
        <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
        {% endif %} {% endfor %}

        <li class="page-item">
            {% if page_obj.has_next %}
            <a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Previous">
                <span aria-hidden="true">&raquo;</span>
            </a>
            {% else %}
        </li>

        <li class="page-item disabled">
            <a class="page-link" href="#" aria-label="Previous">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        {% endif %}
    </ul>
</nav>

After doing some more research I believe the issue is with the next and previous button as they don’t pass any parameters in them other then next page


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.