Django中的分页-原始查询字符串丢失 [英] Pagination in django - the original query string gets lost

查看:54
本文介绍了Django中的分页-原始查询字符串丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用文档中的代码对数据进行分页:

I use the code from the documentation to paginate the data:

try:
    data = paginator.page(request.GET.get('page'))
except PageNotAnInteger:
    page = 1
    data = paginator.page(1)
except EmptyPage:
    data = paginator.page(paginator.num_pages)

还有一个页面:

<div class="pagination">
      <span class="step-links">
          {% if data.has_previous %}
              <a href="?page={{ data.previous_page_number }}">previous</a>
          {% endif %}

          <span class="current">
              <b>Page</b> {{ data.number }} of {{ data.paginator.num_pages }}
          </span>

          {% if data.has_next %}
              <a href="?page={{ data.next_page_number }}">next</a>
          {% endif %}

      </span>
    </div>

但是这里有一个错误:当网址中包含查询字符串并在Pager上单击一下时,原始查询字符串丢失。例如:

But there's a bug here: when the url contains a query string and one clicks on the Pager, the original query string gets lost. For example:

example.com?var1=33&var2=44

,然后单击 page2时,URL变为

and then when one clicks on "page2", the url becomes

example.com?page=2  # var1=33&var2=44 is lost

而不是:

example.com?var1=33&var2=44&page=2 

我既没有找到标准,也没有找到简单的方法来修复它。我该怎么办?

I haven't found neither the standard, nor easy way to fix it. How can I do that?

更新:

当然,名称参数,它们的值以及它们是否存在尚不清楚。

of course, the names of the parameters, their values and whether they exist or not is not known.

推荐答案

您可以直接在请求中访问参数您的模板,如果您在设置中激活 django.core.context_processors.request 。参见 https://docs.djangoproject .com / zh-CN / 1.7 / ref / templates / api /#django-core-context-processors-request

You can access parameters from your request directly in your template if you activate django.core.context_processors.request in your settings. See https://docs.djangoproject.com/en/1.7/ref/templates/api/#django-core-context-processors-request

然后,您可以访问模板中的参数直。根据您的情况,您需要过滤 page 参数。您可以执行以下操作:

Then you can access parameters in your template directly. In your case you'll need to filter page parameter. You could do something like this:

href="?page={{ data.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"

这篇关于Django中的分页-原始查询字符串丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆