我有一个for循环。在内部,我想通过foorloop.counter0访问其他变量(数组)。怎么做到的?我已经尝试过了:{{otherVariable.foorloop.counter0}},但是没有结果。
像这样工作:{{otherVariable.0}}
发布于 2013-09-12 01:17:45
您可以使用自定义模板过滤器(http://djangosnippets.org/snippets/2740/),如下所示
{{ otherVariable|get_at_index:forloop.counter0 }或者使用内置的'slice‘模板标签(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 %}
https://stackoverflow.com/questions/18746523
复制相似问题