I am using Flask render_template method to render my html page as below:
render_template('index.html', content="<div>Hello World!</div>")
My index.html is:
<html lang="en">
<head>
<title>Hello</title>
</head>
<body>
{{content}}
</body>
</html>
My page is replacing the content variable, but it is rendering <div>Hello World!</div> as text.
Is there a way to render html from render_template context variables?
Flask turns on Jinja's autoescape feature. Quoting the manual on how to disable it:
There are three ways to accomplish that:
In the Python code, wrap the HTML string in a Markup object before passing it to the template. This is in general the recommended way.
Inside the template, use the |safe filter to explicitly mark a string as safe HTML ({{ myvariable|safe }})`
Temporarily disable the autoescape system altogether.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With