Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render html content with jinja using flask? [duplicate]

Tags:

flask

jinja2

I'm using Flask in an app that render some data from a sqlite database. My problem is when the app render some text that has html inside, appear as text instead of html. For example, the record in database has the following text:

My tailor <strong>is</strong> rich

The html page render as is:

<html>
<!-- snip .... -->
My tailor &gt;strong&lt;is&gt;/strong&lt; rich
<!-- snip .... -->
</html>

And, what I want is this ("is" word has to be bolder):

<html>
<!-- snip .... -->
My tailor <strong>is</strong> rich
<!-- snip .... -->
</html>

Does anybody know how can I do that?

like image 817
jaloplo Avatar asked Oct 01 '12 11:10

jaloplo


People also ask

What is the difference between Jinja and Jinja2?

Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.

Do you have to use Jinja with flask?

Flask leverages Jinja2 as its template engine. You are obviously free to use a different template engine, but you still have to install Jinja2 to run Flask itself. This requirement is necessary to enable rich extensions.


1 Answers

If you know the content is safe, simply use the safe filter:

{# In the Jinja template #}
{% for article in articles %}
<div class="article">{{article|safe}}</div>
{% endfor %}
like image 85
Sean Vieira Avatar answered Sep 28 '22 02:09

Sean Vieira