Suppress “None” output as string in Jinja2

后端 未结 5 394
无人共我
无人共我 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条回答
  •  囚心锁ツ
    2020-12-07 18:01

    A custom filter can solve the problem. Declare it like this:

    def filter_suppress_none(val):
        if not val is None:
            return val
        else:
            return ''
    

    Install it like this:

    templating_environment.filters['sn'] = filter_suppress_none
    

    Use it like this:

    {{value|sn}}
    

提交回复
热议问题