Pass HTML to template using Flask / Jinja2

I am creating admin for Flask and SQLAlchemy and I want to pass HTML for different inputs in my opinion using render_template . The template script seems to automatically exit html, so all <"'> are converted to html objects. How do I disable this so that the HTML code displays correctly?

+127
python flask jinja2
Jul 08 '10 at 17:34
source share
4 answers

the ideal way is to

 {{ something|safe }} 

Than completely disable auto exposure.

+267
Jul 08 '10 at 17:48
source share

You can also declare it safe HTML from code:

 from flask import Markup value = Markup('<strong>The HTML String</strong>') 

Then pass this value to the templates, and they should not |safe it.

+90
Jul 16 '10 at 16:00
source share

From the Jinja Docs HTML Escape section:

When auto-escaping is enabled, by default all are escaped, except for values ​​explicitly marked as safe. They can be marked with the application or in the template using the filter | safe.

Example:

  <div class="info"> {{data.email_content|safe}} </div> 
+15
Feb 12 '15 at 23:06
source share

If you have many variables that do not need to be escaped, you can use the autoescape block:

 {% autoescape off %} {{ something }} {{ something_else }} <b>{{ something_important }}</b> {% endautoescape %} 
0
Jun 28 '19 at 13:16
source share



All Articles