Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#17230 closed Bug (duplicate)

Broken boolean comparison in template 'if' '==' expression

Reported by: anatoly techtonik <techtonik@…> 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 Preston Holmes, 13 years ago

Resolution: invalid
Status: newclosed

== 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:

{% if closed %} ****** {% endif %}

comment:2 by anatoly techtonik <techtonik@…>, 13 years ago

Resolution: invalid
Status: closedreopened
  1. You missed the fact that I need to distinguish between closed == None and closed == False. How to do this?
  2. 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.

comment:3 by Simon Charette, 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 Preston Holmes, 13 years ago

Resolution: duplicate
Status: reopenedclosed

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})

in reply to:  3 comment:5 by anatoly techtonik <techtonik@…>, 13 years ago

Replying to charettes:

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.

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 Aymeric Augustin, 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 Luke Plant, 13 years ago

UI/UX: unset

We only need one ticket to discuss this, please move discussion to #17229

Note: See TracTickets for help on using tickets.
Back to Top