Djingo Paginator and filters - one breaking the other one

30 Views Asked by At

I have an issue with the configuration of my paginator with filters in the views.py. When the search result pulls many pages of records and that the user clicks on page 1, 2, 3, of the pagination (like going next page), it breaks the filter, and so pulls all records from the database instead of what was filtered.

I am using Bootstrap 5.0.2. Here is my code:

views.py:

def group_search(request):
    if request.method == "GET":
        g_searched = request.GET.get("g_searched", 1)

        # Cache the variable "searched" for views.group_search_csv
        request.session['csv_searched'] = g_searched

        # multiple columns selected in search
        group_searched = Binders.objects.filter(Q(group_id__contains=g_searched))

        group_count = group_searched.count()
        p = Paginator(group_searched, 20)
        page = request.GET.get('page', 1)
        page_obj = p.get_page(page)
        pg_nums = "a" * page_obj.paginator.num_pages

        context = {'g_searched': g_searched, 'page_obj': page_obj, 'pg_nums': pg_nums, 'group_count': group_count}

        return render(request, "group_search.html", context)
    else:
        return render(request, "group_search.html", {}) 

Group_search.html: (I didn't place the code for the entire html page because most of it is irrelevant to this issue.)

<!-- ======= Sub Menu Section ======= -->
 <section class="breadcrumbs">
   <div class="container-fluid">
     <div class="d-flex justify-content-around align-items-center">
        {% if g_searched %}
           <h2>You searched for group {{ g_searched }}</h2>
        {% else %}
           <h2>Search field is empty!</h2>
        {% endif %}
        <!-- added blank spaces before the forms for better spacing-->
        <a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        <form action="{% url 'group_search' %}" class="d-flex" method=GET>
        <!--              {% csrf_token %}-->
        <input aria-label="Group Search" class="form-control me-2 " name="g_searched" placeholder="Search GROUPs" type="search">
        <button class="btn btn-outline-info" type="submit">Search</button>
        </form>
        <!-- added blank spaces after the forms for better spacing-->
        <a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        <ol>
         <li><a href="{% url 'binders' %}">Binders Listing</a></li>
         <li><a href="{% url 'group_search_csv' %}">Download Spreadsheet</a></li>
         <li>Search Result = <b>{{ group_count }}</b></li>
        </ol>
     </div>
  </div>
 </section><!-- End Sub Menu Section -->

<!-- ======= Page Navigation Section ======= -->
<nav aria-label="Page navigation example">
  <ul class="pagination justify-content-center">
    {% if page_obj.has_previous %}
    <li class="page-item"><a class="page-link" href="?page=1">First</a></li>
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
    {% endif %}

    {% for i in pg_nums %}
    <li class="page-item"><a class="page-link" href="?page={{ forloop.counter }}">{{ forloop.counter }}</a></li>
    {% endfor %}

    {% if page_obj.has_next %}
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_core }}">Last</a></li>
    {% endif %}
  </ul>
</nav>

I have looked all over the place to resolve this but haven't found anything yet. If you can see what's wrong in my code, please let me know.

1

There are 1 best solutions below

0
Robin On

I found the solution on a posted video from TauhidCodes here. I tried it and it works exactly as he said. https://www.youtube.com/watch?v=dkJ3uqkdCcY