4
votes

I'm using ListView to display the search result, and using forms to let user input search values, here is the problem: when I get some search results, say, 50 items, and I set the item_per_page 30, so I got two pages, but now when I click the next page link, it forgets the former search request and shows me all of the items in page 2, how to fix this? My code is below:

my form fields are like this:

<form method="get" action="{% url 'query_student' %}">
      #the form fields
</form>       

my pagination template:

{% if is_paginated %}
        <div class="pages">
                {% if page_obj.has_previous %}
                        <a href="/student/querystudent?page={{ page_obj.previous_page_number }}">previous</a>
                {% endif %}
                <span class="btnon">
                        page {{ page_obj.number }}  total {{ page_obj.paginator.num_pages }} 
                </span>
                {% if page_obj.has_next %}
                        <a href="/student/querystudent?page={{ page_obj.next_page_number }}">>next</a>
                {% endif %}
        </div>
{% endif %}
1

1 Answers

0
votes

you are missing query string with all your form data

simplest solution will be to use django-url-tools, then in the links for the previous/next page you will add:

{% load urls %}

...

{% add_params request.get_full_path page=page_obj.previous_page_number %}

...

{% add_params request.get_full_path page=page_obj.next_page_number %}