tmi*_*van 7 django django-admin
django admin中外键过滤器的List_filter标签总是按id排序,当列表中有许多过滤器时,这会导致相当混乱.
我一直在寻找简单的解决方案,如何按字母顺序或按日期订购这些标签一段时间.似乎除了使用FilterSpec之外,没有解决方案.
直到我这样做
我已经更改了filter.html的模板(将它放在模板目录中的admin文件夹中)所以它看起来像这样(在django片段的某处我发现它):
{% load i18n %}
<h3>{% blocktrans with title as filter_title %} By {{ filter_title }} {% endblocktrans %}</h3>
<div align="right">
<select onChange="javascript:window.location = this.options[this.selectedIndex].value;" style="width: 80%">
{% for choice in choices %}
<option {% if choice.selected %}selected{% endif %} value="{{ choice.query_string|iriencode }}">
{{ choice.display }}
</option>
{% endfor %}
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
然后我在for循环中使用'dictsort:"name"'模板标记,所以模板最终看起来像这样:
{% load i18n %}
<h3>{% blocktrans with title as filter_title %} By {{ filter_title }} {% endblocktrans %}</h3>
<div align="right">
<select onChange="javascript:window.location = this.options[this.selectedIndex].value;" style="width: 80%">
{% for choice in choices|dictsort:"display" %}
<option {% if choice.selected %}selected{% endif %} value="{{ choice.query_string|iriencode }}">
{{ choice.display }}
</option>
{% endfor %}
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
我使用了select下拉,因为我有很多标签,但它也可以用在标准的'ul'列表上.现在我终于按字母顺序排列了所有基于外键的过滤器(即使使用日期也能正常工作).
如果你需要反向剂量,那就有dictsortreversed模板标签.
希望这有助于某人.
| 归档时间: |
|
| 查看次数: |
1878 次 |
| 最近记录: |