mpe*_*pen 62 django django-templates
如何使用Django模板语法检查变量是否为False?
{% if myvar == False %}
Run Code Online (Sandbox Code Playgroud)
似乎没有用.
请注意,我非常特别想检查它是否具有Python值False.这个变量也可能是一个空数组,这不是我想要检查的.
Bia*_*cki 34
对于后代,我有几个NullBooleanField,这就是我做的:
检查是否True:
{% if variable %}True{% endif %}
Run Code Online (Sandbox Code Playgroud)
检查它是否有效False(注意这是有效的,因为只有3个值 - 真/假/无):
{% if variable != None %}False{% endif %}
Run Code Online (Sandbox Code Playgroud)
检查是否None:
{% if variable == None %}None{% endif %}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,但我做不到variable == False,但我能做到variable == None.
Mar*_*tin 28
Django 1.10 (发行说明)将标记is和is not比较运算符添加到if标记中.此更改使模板中的身份测试非常简单.
In[2]: from django.template import Context, Template
In[3]: context = Context({"somevar": False, "zero": 0})
In[4]: compare_false = Template("{% if somevar is False %}is false{% endif %}")
In[5]: compare_false.render(context)
Out[5]: u'is false'
In[6]: compare_zero = Template("{% if zero is not False %}not false{% endif %}")
In[7]: compare_zero.render(context)
Out[7]: u'not false'
Run Code Online (Sandbox Code Playgroud)
如果您使用较旧的Django,那么从版本1.5 (发行说明)开始,模板引擎会解释True,False并None作为相应的Python对象.
In[2]: from django.template import Context, Template
In[3]: context = Context({"is_true": True, "is_false": False,
"is_none": None, "zero": 0})
In[4]: compare_true = Template("{% if is_true == True %}true{% endif %}")
In[5]: compare_true.render(context)
Out[5]: u'true'
In[6]: compare_false = Template("{% if is_false == False %}false{% endif %}")
In[7]: compare_false.render(context)
Out[7]: u'false'
In[8]: compare_none = Template("{% if is_none == None %}none{% endif %}")
In[9]: compare_none.render(context)
Out[9]: u'none'
Run Code Online (Sandbox Code Playgroud)
虽然它不像人们预期的那样有效.
In[10]: compare_zero = Template("{% if zero == False %}0 == False{% endif %}")
In[11]: compare_zero.render(context)
Out[11]: u'0 == False'
Run Code Online (Sandbox Code Playgroud)
Zub*_*zal 25
我认为这对你有用:
{% if not myvar %}
Run Code Online (Sandbox Code Playgroud)
Gab*_*ley 23
您可以编写一个自定义模板过滤器来执行以下六行代码:
from django.template import Library
register = Library()
@register.filter
def is_false(arg):
return arg is False
Run Code Online (Sandbox Code Playgroud)
然后在你的模板中:
{% if myvar|is_false %}...{% endif %}
Run Code Online (Sandbox Code Playgroud)
当然,你可以使模板标签更通用......但这特别适合你的需要;-)
{% ifequal YourVariable ExpectValue %}
# Do something here.
{% endifequal %}
Run Code Online (Sandbox Code Playgroud)
{% ifequal userid 1 %}
Hello No.1
{% endifequal %}
{% ifnotequal username 'django' %}
You are not django!
{% else %}
Hi django!
{% endifnotequal %}
Run Code Online (Sandbox Code Playgroud)
与if标记一样,{%else%}子句是可选的.
参数可以是硬编码的字符串,因此以下内容有效:
{%ifequal user.username"adrian"%} ... {%endifequal%} ifequal标记的替代方法是使用if标记和==运算符.
ifnotequal就像ifequal一样,除了它测试两个参数不相等.
ifnotequal标记的替代方法是使用if标记和!=运算符.
{% if somevar >= 1 %}
{% endif %}
{% if "bc" in "abcdef" %}
This appears since "bc" is a substring of "abcdef"
{% endif %}
Run Code Online (Sandbox Code Playgroud)
以上所有可以组合形成复杂的表达式.对于此类表达式,了解在计算表达式时如何对运算符进行分组(即优先级规则)非常重要.运营商的优先级从最低到最高,如下:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/
小智 5
刚刚遇到这个问题(我之前确定过,并提出了一个不太令人满意的解决方案).
对于三态布尔语义(例如,使用models.NullBooleanField),这很有效:
{% if test.passed|lower == 'false' %} ... {% endif %}
Run Code Online (Sandbox Code Playgroud)
或者如果你喜欢对整个事情感到兴奋......
{% if test.passed|upper == 'FALSE' %} ... {% endif %}
Run Code Online (Sandbox Code Playgroud)
无论哪种方式,这都处理了您不关心None(在if块中评估为False)或True大小写的特殊情况.
| 归档时间: |
|
| 查看次数: |
91445 次 |
| 最近记录: |