如何根据指定的规则对GET查询进行排序

时间:2017-04-27 13:22:35

标签: python django

我正在使用url_replace添加GET查询,如下所示。

@register.simple_tag
def url_replace(request, field, value):
    get_params = request.GET.copy()
    get_params[field] = value
    return get_params.urlencode()

<p>[ Category ]</p>
<ul>
    {% for cat in cats %}
    <li><a href="?{% url_replace request 'category' cat.slug %}">{{ cat.name }}</a></li>
    {% endfor %}
</ul>
<p>[ Job Type ]</p>
<ul>
    {% for type_value, type_name in types %}
    <li><a href="?{% url_replace request 'type' type_value %}">{{ type_name }}</a></li>
    {% endfor %}
</ul>

每次点击指定链接时都会安排GET查询。例如:

1)
current URL: /jobs/
click <a href="?{% url_replace request 'category' cat.slug %}">{{ cat.name }}</a>
URL changes: /jobs/?category=cat.slug
then click <a href="?{% url_replace request 'type' type_value %}">{{ type_name }}</a>
URL changes: /jobs/?category=cat.slug&type=type_value

2)
current URL: /jobs/
click <a href="?{% url_replace request 'type' type_value %}">{{ type_name }}</a>
URL changes: /jobs/?type=type_value
then click <a href="?{% url_replace request 'category' cat.slug %}">{{ cat.name }}</a>
URL changes: /jobs/?type=type_value&category=cat.slug

但我想对这些GET查询进行排序,首先是类别,然后是类型。

always: /jobs/?category=cat.slug&type=type_value
not: /jobs/?type=type_value&category=cat.slug

我该怎么做?

0 个答案:

没有答案