在django模板中按索引访问列表

时间:2013-03-26 22:19:51

标签: django django-templates

我正在尝试动态访问列表中的索引位置。

  

{{allAppsList。{{app.id}}。link}}

它不起作用。

  

{{allAppsList.0.link}}

的工作。

应该是一些语法问题。我在线谷歌,但没有找到任何帮助。

顺便说一句: 如果在Java中使用,这就是我想要做的。

  

int index = app.id;

     

String link = appList [index] .getLink();

2 个答案:

答案 0 :(得分:2)

你不能在django模板中做这样的事情,但也不是不可能。

如果您真的喜欢这个,可以创建一个custom template filter。 实施起来很简单,只需将列表和index传递给您的custom filter,然后完全按照Java执行操作。

开始writing custom template filter

答案 1 :(得分:0)

你无法在Django的模板语言中查找类似的列表 - 这是一个设计决策。方法调用,字典键,属性调用和列表索引都由.(点)表示法处理。 Have a look at the docs for more about how Django decides on what to do when it encounters a dot