Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render html for variables in Flask render_template [duplicate]

Tags:

python

flask

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?

like image 362
Awesome Avatar asked Oct 28 '25 00:10

Awesome


1 Answers

Flask turns on Jinja's autoescape feature. Quoting the manual on how to disable it:

There are three ways to accomplish that:

  1. 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.

  2. Inside the template, use the |safe filter to explicitly mark a string as safe HTML ({{ myvariable|safe }})`

  3. Temporarily disable the autoescape system altogether.

like image 179
Ture Pålsson Avatar answered Oct 29 '25 16:10

Ture Pålsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!