1

我有一个 for 循环。在里面我想通过 fooloop.counter0 访问其他变量(数组)。如何?我试过这样:{{otherVariable.foorloop.counter0}} 但没有。

就像这样有效:{{otherVariable.0}}

4

1 回答 1

1

您可以使用自定义模板过滤器(http://djangosnippets.org/snippets/2740/),例如

{{ otherVariable|get_at_index:forloop.counter0 }

或使用内置的“切片”模板标签(https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice

{{otherVariable|slice forloop.counter0 }}

如何定义自定义模板标签/过滤器

参考 - https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#code-layout

您的自定义标签和过滤器将存在于 templatetags 目录中的一个模块中,例如在名为“custom_utils/py”的文件中

在您的模板中,您将使用以下内容: {% load custom_utils %}

于 2013-09-11T17:17:44.690 回答