Chr*_*ris 2 python django django-templates django-models
从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 %}
Run Code Online (Sandbox Code Playgroud)
如果country.grouper是在带有choices = CHOICES_FIELD的模型中声明的Char字段,如何在模板中显示它的详细名称?通常我会采用Model.get_FOO_display,但country.grouper.get_country_list_display当然不会起作用.
自定义模板标签只是选择吗?
你只需要反过来看看它!
使用get_FOO_display
作为分组字段.
{%将get_country_display的城市重新组合为country_list%}
{{country.grouper}}现在将显示选项集中的值字段而不是键.
归档时间: |
|
查看次数: |
976 次 |
最近记录: |