检查是否在django模板语言中定义了变量

时间:2013-07-16 08:20:38

标签: python django django-templates

我正在使用django 1.5,我需要检查一个变量是否被定义(并且如果定义了变量但是它没有用,但是None,0,“”等等)。类似的东西:

{% ifexists a_variable %}
    <p> Hey the variable exists </p>
{% endifexists %}

我不怎么做到这一点......

1 个答案:

答案 0 :(得分:2)

请阅读documentation

  

{%if%}标记评估变量,如果该变量为“true”   (即存在......

{% if athlete_list %}
    Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
    Athletes should be out of the locker room soon!
 {% else %}
    No athletes.
{% endif %}

您可能还想查看writing public views的相当方便的Django教程。

相关问题