【问题标题】:Undesired/wrong URL using Django Paginator使用 Django Paginator 的不需要/错误的 URL
【发布时间】:2021-09-17 05:05:58
【问题描述】:

我正在尝试在我的网页中使用 Django Paginator,但生成的 URL 错误

这是我的代码:

<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 -->

这就是发生的事情:

假设我访问了我的网页 example.com,默认情况下分页页面为 1。

如果我点击第 2 页,我会得到这个网址:

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

如果我点击第 3 页,这是生成的 url:

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

哪个是错的,应该是:

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

另外,如果我点击返回第 1 页,我会得到这个网址:

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

我想知道如何只访问选定的页面而不是保留以前的值。

(简而言之)

我想要什么:

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

我现在得到的:

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

有人知道哪里出了问题吗?

更新:

current_uri 来自这个视图(截断):

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(),
    },
)

【问题讨论】:

  • current_uri是如何生成的?
  • @IainShelvington 编辑了我的问题,但基本上它来自request.get_full_path()

标签: python django django-pagination


【解决方案1】:

使用request.path 而不是request.get_full_path() 代替current_uri,因为它不会包含包含当前“p”参数的当前查询字符串

<a class="page-link" href="{{ request.path }}?p=1">

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2014-12-07
    • 2015-08-28
    • 2016-02-13
    • 1970-01-01
    • 2018-03-23
    • 2019-02-20
    • 1970-01-01
    相关资源
    最近更新 更多