如果django中有相等的陈述,那么写下阶梯

时间:2014-04-18 09:43:11

标签: django django-templates

我知道我可以在django模板中执行此操作

ifequal "this"
  ...//
else
  ...//
endifequal

但我该怎么写呢

ifequal "this"
  ..//
elseifequal "this"
  ...//
else
  ....

在django中有没有一种优雅的方法来实现这个目标?

2 个答案:

答案 0 :(得分:1)

试试这个,

{% ifequal p.gender "M" %}
Bloke
{% else %}
Sheila
{% endifequal %}

ifequal标记确定两个参数是否相等。参数可以是 变量或硬编码字符串。如果参数匹配,则模板引擎呈现 ifequal块内的代码。您需要添加endifequal标记来结束块。

https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#ifequal

答案 1 :(得分:1)

Django模板支持类似Python的elif标记。例如:

{% if this == True %}
  Yes!
{% elif this == False %}
  No!
{% else %}
  What?
{% endif %}

来源:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#if

文档很容易找到,所以我建议你阅读。

相关问题