Lukeyy
Lukeyy

Reputation: 77

Sending a html code/variable from Flask to HTML

Hi I am trying to send a string variable with html code within it and then display this on my website, however it is simply printing the html tags and the text and not displaying it as expected.

My html code for this file (testlogin.html):

<html>
    <head>
    </head>
<body>
    {{ data }}
</body>
</html>

My Python Flask code (app.py):

@app.route('/testindex')
def textLogin():
    data = '<h>Test Header</h><p>Test Paragraph</p>'
    return render_template('testlogin.html', data=data)

Is this even possible to do?

Upvotes: 0

Views: 1767

Answers (1)

simpleApp
simpleApp

Reputation: 3158

when flask sends HTML tags in data, they will be autoescape -- means they will be shown in HTML the way you sent. will be shown as:

&lt;h&gt;Test Header&lt;/h&gt;&lt;p&gt;Test Paragraph&lt;/p&gt;

to render your HTML, you need to enclose your code as follows:

{% autoescape off %}
{{ data }}
{% endautoescape %}

Upvotes: 2

Related Questions