检查是否用Django模板语言定义了变量 [英] Check if a variable is defined in django templating language

查看:123
本文介绍了检查是否用Django模板语言定义了变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.5,我需要检查是否定义了一个变量(如果定义了变量,但是没有,则为0,,等,使其不起作用)。

I am using django 1.5, I need to check if a variable is defined (and have it not work if the variable is defined but None, 0, "", etc...). Something like:

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

我不是最好的选择...

I don't how best to do this...

推荐答案

请阅读文档


{%if%}标记计算变量,如果该变量为 true,则
(即存在...

The {% if %} tag evaluates a variable, and if that variable is "true" (i.e. exists ...



{% 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 %}

您可能还想查看发表公众观点

这篇关于检查是否用Django模板语言定义了变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆