lzhmqq
lzhmqq

Reputation: 35

How to render formatted data to html in Python Flask Framwork?

I have some formatted data in MongoDB, named i:

<p><strong>some string</strong></p>

But when I render it with flask and jinjia, like:

{% for i in example %}
    <div>{{ i }}</div>
{% endfor %}

The Browser show me:

<p><strong>some string</strong></p>

But I want to just get:

some string

I do it using ajax and put the formatted data in html, using jQuery html() method.

But how can I do it just in template rendering part?

Upvotes: 1

Views: 1564

Answers (1)

Andr&#233; Laszlo
Andr&#233; Laszlo

Reputation: 15537

Jinja2 escapes html by default. To mark data as safe for printing as html, use the safe filter.

Either like this:

{{ myvariable|safe }}

Or turn escaping off for a block:

{% autoescape false %}
    <p>autoescaping is disabled here
    <p>{{ will_not_be_escaped }}
{% endautoescape %}

Flask depends on Jinja by default, but you can use a different template engine if you please, but Jinja still needs to be installed.

More information:

Upvotes: 2

Related Questions