Jinja2 template variable if None Object set a default value

后端 未结 9 2066
感情败类
感情败类 2020-12-04 08:33

How to make a variable in jijna2 default to \"\" if object is None instead of doing something like this?

      {% if p %}   
        {{ p.User[\'first_name\'         


        
9条回答
  •  借酒劲吻你
    2020-12-04 08:52

    According to docs you can just do:

    {{ p|default('', true) }}
    

    Cause None casts to False in boolean context.


    Update: As lindes mentioned, it works only for simple data types.

提交回复
热议问题