django模板如果条件 [英] django template if condition

查看:148
本文介绍了django模板如果条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里有一个问题。

我有以下

{% if form.tpl.yes_no_required == True  %}
             <!-- path 1 -->
{% else %}
    {% if form.tpl.yes_no_required == False %}

        <!-- path 2 -->
    {% endif %} 
{% endif %}

form.tpl.yes_no_required是None,但是我被路由到路径2.任何人都可以解释为什么这样做?编辑:如果值不是,我不希望它显示任何东西。

The value for form.tpl.yes_no_required is None, but I was routed to path 2. Can anyone please explain why is this so? if the value is none, i do not want it display anything.

推荐答案

您不能使用模板语言测试你认为是常量,解析器实际上是测试2文字。

You can't use the template language to test against what you think are constants, the parser is actually testing 2 "literals".

解析器测试2个字符,名称为无和假。
当解析器尝试在上下文中解决这些问题时,将抛出一个VariableDoesNotExist异常,并将两个对象解析为python值None
和None == None。

The parser tests for 2 literals with names 'None' and 'False'. When parser tries to resolve these in the context a VariableDoesNotExist exception is thrown and both objects resolve to the python value None and None == None.

from django.template import Context, Template
t = Template("{% if None == False %} not what you think {% endif %}")
c = Context({"foo": foo() })

打印你不是你的想法'

c = Context({'None':None})
t.render(c)

打印u'不是你的想法'

prints u' not what you think '

c = Context({'None':None, 'False':False})
t.render(c)

打印u'

这篇关于django模板如果条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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