I'm building an admin for Flask and SQLAlchemy, and I want to pass the html for the different inputs to my view using render_template
. The templating framework seems to escape the html automatically, so all <"'> are converted to html entities. How can I disable that so that the html renders correctly?
views:
64answers:
3
A:
I hate when you find the answer right after posting a question. Anyway, for reference, you can use
app.jinja_env.autoescape = False
sharvey
2010-07-08 17:40:23
Although this works, ismgopal's solution is definitely the way to go.
sharvey
2010-07-09 02:36:25
+2
A:
the ideal way is to
{{ something|safe }}
than completely turning off auto escaping.
iamgopal
2010-07-08 17:48:14
+2
A:
You can also declare it HTML safe from the code:
from flask import Markup
value = Markup('<strong>The HTML String</strong>')
Then pass that value to the templates and they don't have to |safe
it.
Armin Ronacher
2010-07-16 16:00:19
thanks. |safe works fine for what I use it for. Looks like we have at least 3 ways of doing it.Flask is awesome btw. Great work.
sharvey
2010-07-16 20:07:09
Markup is a Jinja2 class, yes. It implements a common interface supported by many python libraries (unfortunately not Django). You can also use the markup safe package that implements the same object: http://pypi.python.org/pypi/MarkupSafe
Armin Ronacher
2010-07-18 09:02:08