【发布时间】:2012-12-18 03:31:30
【问题描述】:
以 Django 文档中的简单重组示例:
{% regroup cities by country as country_list %}
<ul>
{% for country in country_list %}
<li>{{ country.grouper }}
<ul>
{% for item in country.list %}
<li>{{ item.name }}: {{ item.population }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
如果country.grouper 是在model 中声明的Char 字段,choices=CHOICES_FIELD,我如何在template 中显示它的详细名称?通常我会选择Model.get_FOO_display,但 country.grouper.get_country_list_display 当然不会工作。
自定义模板标签是唯一的选择吗?
【问题讨论】:
-
可以
regroup cities by get_country_display吗? -
记录一下...是的,您可以按照@DanielRoseman 的建议在石斑鱼中使用
get_FOO_display
标签: python django django-models django-templates