我在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中做到这一点吗?
答案 0 :(得分:0)
不幸的是你不能。不用使用那些长名称,您可以这样做
{% 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 %}
如果您想在短绒雷达下飞行,这种解决方法就足够了。
或者您可以在列表中传递这些变量并对其进行迭代
答案 1 :(得分:0)
您不能在if
标记中拆分该表达式,但是可以在python代码中将表达式分配给新变量,然后将其传递给模板。例如
new_var = ABCDEFGH == BENDHSS and asdasd == asdasdas or \
asasdas == asdasdd and dasdasdsa == asdasdass or \
ghgfgsd == efdscsdfg and sgrtvsd == acsdfer:
,然后将new_var
传递给模板。