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

时间:2021-11-27 20:21:51

I am trying to access an index position in list dynamically.

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

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

{ { { app.id } } { allAppsList。链接} }

It is not working.

它不工作。

{{ allAppsList.0.link }}

{ { allAppsList.0。链接} }

worked.

工作。

It should be some grammar issue. I google online, but didn't find anything helpful.

这应该是一些语法问题。我在网上烧烤,但是没有发现任何有用的东西。

BTW: This is what I want to do if using in Java.

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

int index = app.id;

int指数= app.id;

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

(指数)字符串链接= appList .getLink();

2 个解决方案

#1


2  

You can't do such a thing in django template, But it's not impossible also.

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

If you are really into this, you can create a custom template filter. The implementation would be easy, Just pass the list and index to your custom filter and do exactly what you would do with Java.

如果您真的对此感兴趣,您可以创建自定义模板过滤器。实现起来很简单,只需将列表和索引传递到自定义过滤器中,然后就可以像使用Java那样做了。

Start to writing custom template filter

开始编写自定义模板过滤器。

#2


0  

You can't lookup lists like that in Django's template language - it's a design decision. Method calls, dictionary keys, attribute calls and list indexes are all handled by the . (dot) notation. Have a look at the docs for more about how Django decides on what to do when it encounters a dot

在Django的模板语言中,不能像这样查找列表——这是一个设计决策。方法调用、字典键、属性调用和列表索引都由。(点)符号。如果想了解更多关于Django如何在遇到点时决定要做什么,请查看文档

#1


2  

You can't do such a thing in django template, But it's not impossible also.

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

If you are really into this, you can create a custom template filter. The implementation would be easy, Just pass the list and index to your custom filter and do exactly what you would do with Java.

如果您真的对此感兴趣,您可以创建自定义模板过滤器。实现起来很简单,只需将列表和索引传递到自定义过滤器中,然后就可以像使用Java那样做了。

Start to writing custom template filter

开始编写自定义模板过滤器。

#2


0  

You can't lookup lists like that in Django's template language - it's a design decision. Method calls, dictionary keys, attribute calls and list indexes are all handled by the . (dot) notation. Have a look at the docs for more about how Django decides on what to do when it encounters a dot

在Django的模板语言中,不能像这样查找列表——这是一个设计决策。方法调用、字典键、属性调用和列表索引都由。(点)符号。如果想了解更多关于Django如何在遇到点时决定要做什么,请查看文档