Renier
Renier

Reputation: 1535

python if statement always returns False

I am new to the world of Python. I have this 'if statement' in my jinja2 template:

{{context.disabled==True and "Yes" or "No"}}

I am not sure what this syntax is called, but it works like an if.

php equivalent would be: echo $disabled ? "Yes" : "No"

This is always returning No or False even though context.disabled is True,

Why is it behaving like this? I cant see why it is doing this, am I doing it wrong?

Upvotes: 3

Views: 451

Answers (3)

Dennis Sakva
Dennis Sakva

Reputation: 1467

It should be {{ 'Yes' if context.disabled else 'No' }}

Upvotes: 0

alko
alko

Reputation: 48357

Not sure about boolean expressions in jinja templates, but you can use conditions:

{% if context.disabled %} "Yes" {% else %} "No" {% endif %}

Upvotes: 4

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39728

What you want is something like this:

value = "Yes" if context.disabled else "No"

Upvotes: 6

Related Questions