0

For my program in Django I have the following Pagination section in my HTML file

{% if player_list.has_other_pages %}
  <ul class="pagination" style="border: 1px solid #ddd;">
    {% if player_list.has_previous %}
      <li><a href="?ppage={{ player_list.previous_page_number }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><span>&laquo;</span></li>
    {% endif %}
    {% for i in player_list.paginator.page_range %}
      {% with ppage_number=player_list.number radius=8 %}
      {% if i >= ppage_number|sub:radius %}
      {% if i <= ppage_number|add:radius %}
        {% if player_list.number == i %}
          <li class="active" style="border: 1px solid #ddd;"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
        {% else %}
          <li style="border: 1px solid #ddd;"><a href="?ppage={{ i }}">{{ i }}</a></li>
        {% endif %}
      {% endif %}
      {% endif %}
      {% endwith %}
    {% endfor %}
    {% if player_list.has_next %}
      <li><a href="?ppage={{ player_list.next_page_number }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><span>&raquo;</span></li>
    {% endif %}
  </ul>
{% endif %}

So when I press on any of the pagination buttons it calls the page with the search parameter ppage=### (where ### is some number). However, it clears any search parameters I had in the url befarepressing the button, anyway I can let it keep the other search parameters?

This is the views function being called:

.
.
.
# player request parameters
    player_page = request.GET.get('ppage', 1)
    player_first_name = request.GET.get('pfname', None)
    player_last_name = request.GET.get('plname', None)
    player_is_active = request.GET.get('pisactive', None)

    # filter based on search parameters if exist and page
    players = Player.objects.all()
    if (player_first_name != None and player_first_name != ''):
        players = players.filter(first_name__contains=player_first_name)
    if (player_last_name != None and player_last_name != ''):
        players = players.filter(first_name__contains=player_last_name)
    if (player_is_active != None and player_is_active != ''):
        players = players.filter(first_name__contains=player_is_active)
    player_paginator = Paginator(players, 50)
.
.
.

If the url had ?pfname=Chris, after pressing the button the search parameter disappears

Izak
  • 23
  • 5

1 Answers1

0

You will need to pass the QueryDict back to the template and rerender the querystring, except for the page part, so:

def my_view(request):
    qd = request.GET.copy()
    qd.pop('ppage', None)
    # …
    context = {
        # …,
        'qd': qd
    }
    return render(request, 'name-of-template.html', context)

In the template, you then render the urls with:

<a href="?ppage={{ i }}&{{ qd.urlencode }}">

and similar for all the other links.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555