【问题标题】:Django URL parameters replace each otherDjango URL 参数相互替换
【发布时间】:2018-12-01 03:46:20
【问题描述】:

也许我的问题看起来有些简单易懂,但即便如此。

在我的项目中,我使用标准的分页和排序。

问题是它们在获取请求中相互替换,例如我对它们进行了排序,如果我转到第二页,则该集合未排序。我明白原因,似乎答案就在上面,但即使这样我也找不到。

排序:

<a href="?order_by=counter__service__name_service&sort={{ sort_type }}">Counter</a>

分页示例:

<a class="page-link" href="?page={{ history_application.previous_page_number }}">&laquo;</a>

问题是如何保存之前传输的参数。

【问题讨论】:

  • 您需要将它们添加到您在模板中生成的任何链接中。
  • @Daniel Roseman 添加我想保留的参数?如果是这样的话。分配给他们的价值是什么。喜欢“page={{request.GET.page}}&”?

标签: django django-urls get-request


【解决方案1】:

尝试使用更新当前request.GET参数并使用urllib.urlencode添加新参数的Django模板标签。

创建模板标签:

# templatetags.app_tags

import urllib

@register.simple_tag(takes_context=True)
def url_add_query(context, **kwargs):
    """ Updates the current path from existing GET parameters. """
    request = context.get('request')

    get = request.GET.copy()
    get.update(kwargs)
    return u'{path}?{params}'.format(path=request.path,
                params=urllib.urlencode(get, 'utf-8'))

在您的 HTML 模板上:

{% load app_tags %}

<a href="{% url_add_query page=history_application.previous_page_number %}">&laquo;</a>

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 2015-02-18
    • 2011-07-31
    • 2015-02-07
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多