python - Django 模板中的多行 if 条件

标签 python django django-templates

我在 Django 模板中的 if 语句太长。我想把它分成多行。

    {% if ABCDEFGH == BENDHSS and asdasd == asdasdas or asasdas == asdasdd and dasdasdsa == asdasdass or ghgfgsd == efdscsdfg and sgrtvsd == acsdfer %}

我想把上面这行代码分成多行。 在 python 中,我们用反斜杠 ("\") 来分隔它。

    if ABCDEFGH == BENDHSS and asdasd == asdasdas or \
        asasdas == asdasdd and dasdasdsa == asdasdass or \
        ghgfgsd == efdscsdfg and sgrtvsd == acsdfer:

谁能告诉我我们如何在 django 中做到这一点?

最佳答案

不幸的是你不能。您可以这样做,而不是使用那些长名称

{% with a as ABCDEFGH %}
{% with b as BENDHSS %}
{% with c as asdasdas %}
.
.
.
.

   {% if a == b and asdasd == c or asasdas == asdasdd and dasdasdsa == asdasdass or ghgfgsd == efdscsdfg and sgrtvsd == acsdfer %}

{% endwith %}
{% endwith %}
{% endwith %}

如果您想在 linters 雷达下飞行,这个解决方法就足够了。


或者您可以将这些变量传递到列表中并遍历它们

关于python - Django 模板中的多行 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51157698/

相关文章:

django - "Reverse for ... not found"- 但有吗?

django - 使用 {% url ??? %} 在 Django 模板中

python - Django 忽略 mysql 连接错误

javascript - 如何通过 Selenium 查找由 javascript 生成的标签(我猜)

javascript - python3 opencv 不保存可读视频

django - 类型错误 : autodiscover_tasks() missing 1 required positional argument: 'packages' . celery

python - Django 类别及其子类别

python - Jupyter 是否支持 'read-only' 笔记本?

python - 如何在 ubuntu 18.04 bionic 上安装加密技术?

javascript - 设计一个使用 forloop.counter 的 Div