Reputation: 2657
I'm working in a Django template and I want to check the value of a boolean and print 'bold' if it's True, and 'plain' if it's False. If the value is blank or not defined, I want to treat it as if it is True. Here's what I'm currently using:
{{ something.boolean_val|yesno:"bold,plain,bold" }}
However, when something.boolean
isn't defined, it's treating it as False, not None. I tried adding a |default_if_none
filter, but that did nothing. When I tried |default
, it changed False values as well.
Delving deeper into the docs, it seems that an undefined variable is set to ''
by default, which gets treated as False. I only want to treat an undefined variable as True (None would also be fine) in this particular situation, so I don't want to fiddle with TEMPLATE_STRING_IF_INVALID
.
Is there a way to make this treat an undefined variable as True? Alternatively, is there a way I could distinguish ''
from False?
Upvotes: 0
Views: 677
Reputation: 439
I don't see it as a filter but in the template language logic you can use '==' which equates to '===' or 'is' in python and does not match None or '' to False the way that using 'not' does.
{% if something.boolean_val == False %}plain{% else %}bold{% endif %}
Upvotes: 1