如果没有对象设置默认值,则Jinja2模板变量。

时间:2022-03-20 20:46:22

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

如何在jijna2默认设置中创建一个变量,如果对象是None,而不是这样做?

      {% if p %}   
        {{ p.User['first_name']}}
      {% else %}
        NONE
      {%endif %}

So if object p is None I want to default the values of p (first_name and last_name) to "". Basically nvl(p.User[first_name'], "")

因此,如果对象p不是我想要的,我要默认的值是p (first_name和last_name)。基本上nvl(p。用户[first_name '],“”)

Error receiving: Error: jinja2.exceptions.UndefinedError UndefinedError: 'None' has no attribute 'User'

接收错误:错误:jinja2.exceptions。UndefinedError UndefinedError:“None”没有属性“User”

8 个解决方案

#1


120  

Use the none builtin function (http://jinja.pocoo.org/docs/templates/#none):

使用none内置函数(http://jinja.pocoo.org/docs/templates/#none):

{% if p is not none %}   
    {{ p.User['first_name'] }}
{% else %}
    NONE
{%endif %}

or

{{ p.User['first_name'] if p != None else 'NONE' }}

or if you need an empty string:

或者如果你需要一个空字符串:

{{ p.User['first_name'] if p != None }}

#2


65  

{{p.User['first_name'] or 'My default string'}}

#3


39  

According to docs you can just do:

根据你的文档,你可以这样做:

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

Cause None casts to False in boolean context.

因为在布尔环境中,没有任何类型转换为False。


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

更新:正如lindes所提到的,它只适用于简单的数据类型。

#4


17  

As addition to other answers, one can write something else if variable is None like this:

除了其他答案之外,如果变量不是这样的话,可以写一些别的东西:

{{ variable or '' }}

#5


5  

Following this doc you can do this that way:

跟随这个医生,你可以这样做:

{{ p.User['first_name']|default('NONE') }}

#6


1  

For Google visitors, not the OP.

If the default value you want is just spaces to maintain alignment, you can use the builtin format filter:

如果您想要的默认值只是保持对齐的空间,您可以使用builtin格式过滤器:

>>> import jinja2
>>> T = jinja2.Template("foo {{ '%-15s'|format(bar) }} baz")
>>> T.render()
'foo                 baz'
>>> T.render({'bar': ''}) # this is the same as above, except explicit
'foo                 baz'
>>> T.render({'bar': 'spam'})
'foo spam            baz'

#7


0  

To avoid throw a exception while "p" or "p.User" is None, you can use:

避免在“p”或“p”时抛出异常。用户“没有,您可以使用:

{{ (p and p.User and p.User['first_name']) or "default_value" }}

#8


0  

I usually define an nvl function, and put it in globals and filters.

我通常定义一个nvl函数,并把它放在全局和过滤器中。

def nvl(*args):
    for item in args:
        if item is not None:
            return item
    return None

app.jinja_env.globals['nvl'] = nvl
app.jinja_env.filters['nvl'] = nvl

Usage in a template:

使用模板:

<span>Welcome {{ nvl(person.nick, person.name, 'Anonymous') }}<span>

// or 

<span>Welcome {{ person.nick | nvl(person.name, 'Anonymous') }}<span>

#1


120  

Use the none builtin function (http://jinja.pocoo.org/docs/templates/#none):

使用none内置函数(http://jinja.pocoo.org/docs/templates/#none):

{% if p is not none %}   
    {{ p.User['first_name'] }}
{% else %}
    NONE
{%endif %}

or

{{ p.User['first_name'] if p != None else 'NONE' }}

or if you need an empty string:

或者如果你需要一个空字符串:

{{ p.User['first_name'] if p != None }}

#2


65  

{{p.User['first_name'] or 'My default string'}}

#3


39  

According to docs you can just do:

根据你的文档,你可以这样做:

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

Cause None casts to False in boolean context.

因为在布尔环境中,没有任何类型转换为False。


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

更新:正如lindes所提到的,它只适用于简单的数据类型。

#4


17  

As addition to other answers, one can write something else if variable is None like this:

除了其他答案之外,如果变量不是这样的话,可以写一些别的东西:

{{ variable or '' }}

#5


5  

Following this doc you can do this that way:

跟随这个医生,你可以这样做:

{{ p.User['first_name']|default('NONE') }}

#6


1  

For Google visitors, not the OP.

If the default value you want is just spaces to maintain alignment, you can use the builtin format filter:

如果您想要的默认值只是保持对齐的空间,您可以使用builtin格式过滤器:

>>> import jinja2
>>> T = jinja2.Template("foo {{ '%-15s'|format(bar) }} baz")
>>> T.render()
'foo                 baz'
>>> T.render({'bar': ''}) # this is the same as above, except explicit
'foo                 baz'
>>> T.render({'bar': 'spam'})
'foo spam            baz'

#7


0  

To avoid throw a exception while "p" or "p.User" is None, you can use:

避免在“p”或“p”时抛出异常。用户“没有,您可以使用:

{{ (p and p.User and p.User['first_name']) or "default_value" }}

#8


0  

I usually define an nvl function, and put it in globals and filters.

我通常定义一个nvl函数,并把它放在全局和过滤器中。

def nvl(*args):
    for item in args:
        if item is not None:
            return item
    return None

app.jinja_env.globals['nvl'] = nvl
app.jinja_env.filters['nvl'] = nvl

Usage in a template:

使用模板:

<span>Welcome {{ nvl(person.nick, person.name, 'Anonymous') }}<span>

// or 

<span>Welcome {{ person.nick | nvl(person.name, 'Anonymous') }}<span>