#17230 closed Bug (duplicate)
Broken boolean comparison in template 'if' '==' expression
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Template system | Version: | 1.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I pass closed
boolean variable to template, which can have three values - None, True, False. I want string *****
to show only if closed is True.
_{{closed}}_ {%if closed == True%} ***** {%endif%} {%if closed == 'True'%} ***** {%endif%}
I am surprised to see that *****
output twice when closed
is None, and doesn't show if closed
boolean is set regardless of its value. I expect numerous complaints about that, but couldn't any open reports, so I imagine it is 'feature' of template engine, but for me such ugly feature is a bug.
Change History (7)
comment:1 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 13 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
- You missed the fact that I need to distinguish between
closed == None
andclosed == False
. How to do this? - AFAIK is a poor excuse. It standard boolean literals are not allowed in expressions while string and integer literals are, then it should be documented properly at least.
follow-up: 5 comment:3 by , 13 years ago
techtonik, I know you stated you need to distinguish between closed == None
and closed == False
in #17229 but I can't find that statement in the above description. You only state that you want string * * * * to show only if closed is True. which is the question ptone answered to.
Why not use the solution proposed by lukeplant in #17229
{% if closed == None %} Works since None is an undefined variable {% else %}{% if closed %} True {% else %} False {% endif %}{% endif %}
IMHO the main reason why boolean expressions are not allowed in there is because they are not mandatory. Don't you think that {% if closed %
} makes more sense than {% if closed == True %
}? It might be the reason why not so many people stumbled on this.
comment:4 by , 13 years ago
Resolution: | → duplicate |
---|---|
Status: | reopened → closed |
at its core, this is really a dupe of #17229
If you want something like this while the eval of those keywords is considered, just do:
context.update({'true':True, 'false':False, 'none': None})
comment:5 by , 13 years ago
Replying to charettes:
techtonik, I know you stated you need to distinguish between
closed == None
andclosed == False
in #17229 but I can't find that statement in the above description. You only state that you want string * * * * to show only if closed is True. which is the question ptone answered to.
If you don't strip my quote - you'll get a chance to read it right. I pass closed boolean variable to template, which can have three values - None, True, False. I want string * to show only if closed is True.
Why not use the solution proposed by lukeplant in #17229
{% if closed == None %} Works since None is an undefined variable {% else %}{% if closed %} True {% else %} False {% endif %}{% endif %}
Because like he said - it works 'by accident'.
IMHO the main reason why boolean expressions are not allowed in there is because they are not mandatory. Don't you think that
{% if closed %
} makes more sense than{% if closed == True %
}? It might be the reason why not so many people stumbled on this.
I think that Django supports NullBooleanField in model, but doesn't in template, where {% if closed == None %}
makes perfect sense.
It also might be the reason why some people think that Django templating suxx. =) Knowing that there are rumors that it is too conservative to change things, I assume that these people didn't even bother to report. I probably wouldn't either if Trac required me to register or remember pass.
comment:6 by , 13 years ago
We don't want to support tri-valued boolean logic, especially in the template engine, which was designed with simplicity in mind. It's bad enough in Python code.
comment:7 by , 13 years ago
UI/UX: | unset |
---|
We only need one ticket to discuss this, please move discussion to #17229
== is a strict comparison, True in your first line is an undefined variable in the context sense, so None == None the True keyword isn't valid in a template as far as I know
What you are looking for is just: