Natiq Vahabov
Natiq Vahabov

Reputation: 504

django pagination returns all data after clicking any page in search result

I paginate search result. At first, query set gives me perfect result and initial page numbers indicate correctly, but when click to any page number, it accepts search key as null and get all data from db. here is the code

def search(request):
    searchKey = request.POST.get("key", "")
    searchRequestsInitial = ScRequests.objects.filter(description__contains=searchKey)
    searchCount = searchRequestsInitial.count()

    page = request.GET.get('page', 1)
    paginator = Paginator(searchRequestsInitial, 10)

    try:
        searchRequests = paginator.page(page)
    except PageNotAnInteger:
        searchRequests = paginator.page(1)
    except EmptyPage:
        searchRequests = paginator.page(paginator.num_pages)

    context = {'searchRequests':searchRequests,'searchCount':searchCount,'searchKey':searchKey}
    template = "searchResult.html"
    return render(request,template,context)

in template:

    <tbody>
    {% if searchRequests %}
       {% for searchRequest in searchRequests %}
        <tr>
          <td>{{searchRequest.description|convert_utf8|shortName}}</td>
          <td>{{searchRequest.title|convert_utf8|shortName}}</td>
          <td>{{searchRequest.sender}}</td>
        </tr>
      {% endfor %}
    {% endif %}  
      </tbody>
    </table>

    {% if searchRequests.has_other_pages %}
      <ul class="pagination">
        {% if searchRequests.has_previous %}
          <li><a href="?page={{ searchRequests.previous_page_number }}">&laquo;</a></li>
        {% else %}
          <li class="disabled"><span>&laquo;</span></li>
        {% endif %}
        {% for i in searchRequests.paginator.page_range %}
          {% if searchRequests.number == i %}
            <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
          {% else %}
            <li><a href="?page={{ i }}">{{ i }}</a></li>
          {% endif %}
        {% endfor %}
        {% if searchRequests.has_next %}
          <li><a href="?page={{ searchRequests.next_page_number }}">&raquo;</a></li>
        {% else %}
          <li class="disabled"><span>&raquo;</span></li>
        {% endif %}
      </ul>
    {% endif %}

Upvotes: 2

Views: 752

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47364

You forget to add key argument to the url:

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

so when you click link key value sets to null. Change links like this:

<li><a href="?page={{ searchRequests.previous_page_number }}&key={{ searchKey }}">&laquo;</a></li>    

UPDATE

Also when you open page via link you are using GET, so add also this:

searchKey = request.POST.get("key", "")
if not searchKey:
    searchKey = request.GET.get("key", "")     

it will set key from request's GET if POST is empty.

But instead of this you can change search form method to GET, and now you can simply check GET argument:

searchKey = request.GET.get("key", "")

Upvotes: 2

Related Questions