ljs.dev
ljs.dev

Reputation: 4493

Print None object properties as empty string

I am passing an object to my Jinja2 template to be used as form values.

If a property doesn't exist, it is printing None as a string, where I would like it to just print an empty string (aka, nothing).

As there are a lot of properties for the object, I wish to avoid string coercion at the Controller level.

My current Jinja code looks like:

value="{{ my_object.my_property }}"

Upvotes: 2

Views: 584

Answers (2)

ljs.dev
ljs.dev

Reputation: 4493

I was able to utilize the following, which is not too long:

{{ my_object.my_property or '' }}

Upvotes: 1

fasouto
fasouto

Reputation: 4511

Try:

{{ my_object.my_property if my_object.my_property != None }}

Upvotes: 1

Related Questions