Text escaped when I want it to show up as html in Flask/jinja2

后端 未结 2 886
猫巷女王i
猫巷女王i 2020-12-29 04:01

I pull a feed from rss and store the data in a appengine db. The rss feed content includes the entire html. So I have this python code:

@app.route(\"/rssRe         


        
相关标签:
2条回答
  • 2020-12-29 04:41

    Instead of data=Markup(feedItem.html).unescape(), you should be using data=Markup(feedItem.html). That will do the right thing and keep your template clean.

    Calling unescape() here is pointless (unless feeditem.html contains pre-escaped html, which it probably doesn't). More importantly, using unescape() here produces a string/unicode object instead of a Markup object, which keeps Jinja2 from recognizing that the field contains html that needs escaping. This defeats Jinja2's automatic escaping ability (that's the purpose of the Markup class!) I also forces your future template maintainers to remember that this field requires manual escaping, which clutters the template code with extra calls.

    0 讨论(0)
  • 2020-12-29 04:55

    This should work too.

    {% extends "layout.html" %}
    {% block body %}
    {{ data|safe }}
    {% endblock %}
    
    0 讨论(0)
提交回复
热议问题