在 Django include 标签中动态生成一个值

Dynamically generate one value in a Django include tag

我有一个 include 标签,如果我对值进行硬编码就可以正常工作,但我需要使用 {{ foo_counter }} 即时生成。这样做:

{% include "template.html" with foo=var1 pos="var_num_{{foo_counter.next}}" bool="0" %}

只按原样输出字符串而不解释变量(如我所料)。我已经尝试了围绕它的 with 标记的一些组合,但我得到了模板错误或相同的输出。

您可以使用 Django 的内置 add 过滤器:https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#add

{% include "template.html" with foo=var1 pos="var_num_"|add:foo_counter.next bool="0" %}