我正在使用 regroup模板标签对选择字段上的查询集输出进行分组。在模型中:
RESOURCE_TYPES = (
('tut','External tutorial'),
('read','Additional reading'),
('org','Company or organization'),
)
restype = models.CharField('Resource type',max_length=6,choices=RESOURCE_TYPES)
在 View 中:
resources = Resource.objects.filter(tutorial=tutorial)
在模板中:
{% regroup resources by restype as resource_list %}
{% for type in resource_list %}
<h3>{{type.grouper}}</h3>
因此 type.grouper 在页面上呈现为“tut”或“org”,而不是长格式。通常你会使用 get_foo_display语法来获取选择的值,而不是键。但是经过重组后似乎没有值(value)。我找不到在 {{type.grouper}} 上使用 get_foo_display 的方法。
当您考虑它时,这是有道理的,但是解决方法是什么?谢谢。
最佳答案
如果你这样做会发生什么
{% regroup resources by get_restype_display as resource_list %}
关于django - 在 Django 模板中将 regroup 与 get_foo_display 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2741645/