python - 在django中,我可以在if标签中使用 'as'吗,比如: {% if excessively_verbose. chain_of_long.nested_references as foo%}

标签 python django if-statement django-templates

我使用 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%}

但是我在 is 末尾未使用 with/as

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

我猜这在 Django 中相当于 this ,这看起来前景并不乐观

最佳答案

听起来你想要的是更多的东西:

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

关于python - 在django中,我可以在if标签中使用 'as'吗,比如: {% if excessively_verbose. chain_of_long.nested_references as foo%},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30331879/

相关文章:

python - 从数据集中以线的形式提取簇

django - 如何在 Django 中处理动态图像?

python - (Python 2.7) easygui.choicebox if else 语句如何

Python:从网页中抓取视频(在 HTML 中不可见)

python - 理解Python中的枚举

python - 闲置一段时间后的性能

python - Django 图片上传不上传日语命名图片

python - django-1.8 : "XXXView"object has no attribute 'add_error'

windows - 在批处理文件的 if/else 语句中使用 ")"

c++ - 清理多个 if 语句 C++