Suppress “None” output as string in Jinja2

后端 未结 5 405
无人共我
无人共我 2020-12-07 17:40

How do I persuade Jinja2 to not print \"None\" when the value is None?

I have a number of entries in a dictionary and I would like to outpu

5条回答
  •  Happy的楠姐
    2020-12-07 17:58

    Another option is to use the finalize hook on the environment:

    >>> import jinja2
    >>> e = jinja2.Environment()
    >>> e.from_string("{{ this }} / {{ that }}").render(this=0, that=None)
    u'0 / None'
    

    but:

    >>> def my_finalize(thing):
    ...     return thing if thing is not None else ''
    ...
    >>> e = jinja2.Environment(finalize=my_finalize)
    >>> e.from_string("{{ this }} / {{ that }}").render(this=0, that=None)
    u'0 / '
    

提交回复
热议问题