0

I'm trying to use Django Paginator in my webpage but the generated URL is wrong

Here is my code:

<div class="row">
  <div class="col-auto">
    <ul class="pagination">
      {% if tickets.has_previous %}
      <li class="page-item"><a class="page-link" href="{{current_uri}}?p=1">&lt;&lt;</a></li>
      <li class="page-item"><a class="page-link" href="{{current_uri}}?p={{ tickets.previous_page_number }}">&lt;</a></li>
      {% else %}
      <li class="page-item disabled"><a class="page-link" href="">&lt;&lt;</a></li>
      <li class="page-item disabled"><a class="page-link" href="">&lt;</a></li>
      {% endif %}
      {% for page_number in tickets.paginator.page_range|pagination_limit:tickets.number %}
      {% ifequal tickets.number page_number %}
      <li class="page-item active"><a class="page-link" href="{{current_uri}}?p={{page_number}}">{{page_number}}</a></li>
      {% else %}
      <li class="page-item"><a class="page-link" href="{{current_uri}}?p={{page_number}}">{{page_number}}</a></li>
      {% endifequal %}
      {% empty %}
      No pages
      {% endfor %}
      {% if tickets.has_next %}
      <li class="page-item"><a class="page-link" href="{{current_uri}}?p={{ tickets.next_page_number }}">&gt;</a></li>
      <li class="page-item"><a class="page-link" href="{{current_uri}}?p={{ tickets.paginator.num_pages }}">&gt;&gt;</a></li>
      {% else %}
      <li class="page-item disabled"><a class="page-link" href="">&gt;</a></li>
      <li class="page-item disabled"><a class="page-link" href="">&gt;&gt;</a></li>
      {% endif %}
    </ul>
  </div> <!-- .col-auto -->
</div> <!-- .row -->

And this is what happened:

Let's say I visit my webpage, example.com and by default the pagination page is 1.

If I click on page 2, I'll get this url:

https://example.com/tasks/?p=2

If I click on page 3, this is the generated url:

https://example.com/tasks/?p=2?p=3

Which is wrong, and it should be:

https://example.com/tasks/?p=3

Also if I click back to page 1, I'm getting this url:

https://example.com/tasks/?p=2?p=3?p=1

I would like to know how to visit only the selected page instead of keeping the previous values.

(In short)

What I want:

https://example.com/tasks/ https://example.com/tasks/?p=2 https://example.com/tasks/?p=3 ...

What I am getting right now:

https://example.com/tasks/ https://example.com/tasks/?p=2 https://example.com/tasks/?p=2?p=3

anyone have any idea of what can be wrong?

UPDATE:

current_uri comes from this view (truncated):

return render(
    request,
    "project/tickets.html",
    {
        "tickets_count": total_count,
        "tickets_filter_count": filtered_count,
        "tickets": tickets,
        "filter": filter_set,
        "filter_project": filter_project,
        "current_uri": request.get_full_path(),
    },
)
Cheknov
  • 1,892
  • 6
  • 28
  • 55

1 Answers1

0

Use request.path instead of request.get_full_path() for current_uri as it will not include the current querystring that includes the current "p" parameter

<a class="page-link" href="{{ request.path }}?p=1">
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50