Allowing
tags with Google App Engine and Jinja2

后端 未结 8 1719
粉色の甜心
粉色の甜心 2021-01-04 22:11

In my web app, the user can make blog posts. When I display the blog post, newlines aren\'t shown because I didn\'t replace the new lines with
tags.

8条回答
  •  失恋的感觉
    2021-01-04 22:31

    Here's a filter wrote by myself:

    import jinja2
    
    @jinja2.evalcontextfilter
    def nl2br(eval_ctx, value):
        result = jinja2.escape(value).unescape().replace('\n', '
    ') if eval_ctx.autoescape: result = jinja2.Markup(result) return result

    And add the filter to the jinja2.Environment() by calling:

    jinja_env.filters['nl2br'] = nl2br
    

提交回复
热议问题