如果object为None,如何使jijna2中的变量缺省为"“,而不是这样做?
{% if p %}
{{ p.User['first_name']}}
{% else %}
NONE
{%endif %}
因此,如果对象p为None,我希望将p (first_name和last_name)的值默认为"“。基本上
nvl(p.User[first_name'], "")
接收时出错:
Error: jinja2.exceptions.UndefinedError
UndefinedError: 'None' has no attribute 'User'
发布于 2013-10-27 16:28:02
使用none
test (不要与Python的None
object混淆!):
{% if p is not none %}
{{ p.User['first_name'] }}
{% else %}
NONE
{% endif %}
或者:
{{ p.User['first_name'] if p is not none else 'NONE' }}
或者,如果您需要一个空字符串:
{{ p.User['first_name'] if p is not none }}
发布于 2014-06-24 19:46:59
{{p.User['first_name'] or 'My default string'}}
发布于 2015-03-24 03:04:06
https://stackoverflow.com/questions/19614027
复制相似问题