django模板if条件

django模板if条件,django,django-templates,Django,Django Templates,这里有个问题 我有以下几点 {% if form.tpl.yes_no_required == True %} <!-- path 1 --> {% else %} {% if form.tpl.yes_no_required == False %} <!-- path 2 --> {% endif %} {% endif %} {%if form.tpl.yes\u no\u required==True

这里有个问题

我有以下几点

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

        <!-- path 2 -->
    {% endif %} 
{% endif %}
{%if form.tpl.yes\u no\u required==True%}
{%else%}
{%if form.tpl.yes\u no\u required==False%}
{%endif%}
{%endif%}

form.tpl.yes\u no\u required的值为None,但我被路由到路径2。谁能解释一下为什么会这样?编辑:如果值为“无”,我不希望它显示任何内容。

无!=假无!=也是这样。。。 做一些像这样的事情,没有项目

{% if form.tpl.yes_no_required  %}
             <!-- path 1 -->
{% else %}
    {% if not form.tpl.yes_no_required %}

        <!-- path 2 -->
    {% endif %} 
{% endif %}
{%if form.tpl.yes\u no\u required%}
{%else%}
{%if not form.tpl.yes\u no\u required%}
{%endif%}
{%endif%}

您不能使用模板语言来测试您认为是常量的内容,解析器实际上在测试2个“文本”

解析器测试两个名为“None”和“False”的文本。 当解析器试图在上下文中解析这些对象时,将抛出variabledesnotexist异常,并且两个对象都解析为python值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)
c = Context({'None':None, 'False':False})
t.render(c)
打印你“不是你想的”

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

打印u“”

我希望我的代码不进入路径2,但奇怪的是我的代码进入路径2。@业余爱好者可以在python中检查它,如果为“无”,则将其类型更改为false实际上我需要所有3个值,true false和none。@业余爱好者因此在此处使用{%if form.tpl.yes\u no\u required%}检查它。。。不管有没有