views:

64

answers:

3

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?

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
Although this works, ismgopal's solution is definitely the way to go.
sharvey
+2  A: 

the ideal way is to

{{ something|safe }}

than completely turning off auto escaping.

iamgopal
Thanks, it works perfectly, and does in fact feel a lot safer.
sharvey
+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
does markup exist in jinja2 ?
iamgopal
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
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