在 Django 中,我可以在 if 标签中使用 'as' 吗?{% if excessively_verbose.chain_of_long.nested_references as foo%}

In django, can I use 'as' in if tags like so: {% if excessively_verbose.chain_of_long.nested_references as foo%}

我用 excessively_verbose.chain_of_long.nested_references.

从 json 填充 xml

有时父级别不存在,为了减少模板的填充,我想使用类似这样的东西:

{% if excessively_verbose.chain_of_long.nested_references as foo%}

我已经尝试了上面和下面的方法:

{% if excessively_verbose.chain_of_long.nested_references with excessively_verbose.chain_of_long.nested_references as foo%}

但我得到 unused with/as at end of is

这可能吗(最好不要重写 {% if %} 标签)?它看起来简单、直接且有用(至少对我而言)!

我想这是 this 的 Django 等价物,看起来不太好

听起来你想要的更像是:

{% with foo=excessively_verbose.chain_of_long.nested_references %}
    {% if foo %}
        {{ foo }} bar
    {% endif %}
{% endwith %}