0

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?

2
  • But why? Just use an include if you really don't want to put your html there. Commented Apr 21, 2021 at 15:06
  • Does this answer your question? Proper way to insert HTML into Flask Commented Apr 21, 2021 at 15:07

1 Answer 1

2

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 %}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.