Allowing
tags with Google App Engine and Jinja2

后端 未结 8 1669
粉色の甜心
粉色の甜心 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:32

    In your model object, add a function like this:

    class Post(db.Model):
        # ...
    
        def html_content(self):
            # Escape, then convert newlines to br tags, then wrap with Markup object
            # so that the 
    tags don't get escaped. def escape(s): # unicode() forces the conversion to happen immediately, # instead of at substitution time (else
    would get escaped too) return unicode(jinja2.escape(s)) return jinja2.Markup(escape(self.content).replace('\n', '
    '))

    Then in your template, just call that:

    {{ post.html_content() }}

提交回复
热议问题