Django Templating:如何访问列表中第一个项目的属性 [英] Django Templating: how to access properties of the first item in a list
本文介绍了Django Templating:如何访问列表中第一个项目的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很简单我有一个Python列表,我传递给一个Django模板。
我可以使用
$ b来专门访问此列表中的第一个项目$ b
{{thelist | first}}
但是,我也想访问该项目的财产...理想情况下,您会认为这样:
{{thelist | first.propertyName}}
但唉,没有。 b
$ b
是否有任何模板解决方案,或者我只是想找到自己传递一个额外的模板变量...
解决方案
您可以通过索引号访问列表中的任何项目。在一个模板中,它与任何其他属性查找一样:
{{thelist.0.propertyName}}
Pretty simple. I have a Python list that I am passing to a Django template.
I can specifically access the first item in this list using
{{ thelist|first }}
However, I also want to access a property of that item... ideally you'd think it would look like this:
{{ thelist|first.propertyName }}
But alas, it does not.
Is there any template solution to this, or am I just going to find myself passing an extra template variable...
解决方案
You can access any item in a list via its index number. In a template this works the same as any other property lookup:
{{ thelist.0.propertyName }}
这篇关于Django Templating:如何访问列表中第一个项目的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文