How to write Inner HTML with Flask?

Issue

I need to add some text in a HTML file, using Flask, with HTML tags enabled.
If the HTML file looks like {{name}}, I can use flask like this:

@app.route('/')
def main():
    return render_template('template.html', name='name')

However, if I try the code below, the text between the <b> tag isn’t bold.

@app.route('/')
def main():
    return render_template('template.html', name='<b>name</b>')

How can I change this?

Solution

You could try using

{{ name | safe }}

Is there a reason you can’t / don’t want to do <b>{{ name }}</b>? Do you want different formatting depending on the name? In the case you could also use css and classes that depend on the name.

Answered By – Max Leidl

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published