Flask - Render HTML using the safe filter
by
Jeremy Canfield |
Updated: September 10 2023
| Flask articles
Let's say you are querying data from a SQL table using SQLAlchemy, perhaps something like this.
@blueprint.route('/Results', methods=['GET', 'POST'])
def results():
id = request.form.get('id')
data = mytable.query.filter_by(id=id)
return render_template('results.html', result=data)
And then in your HTML page, you are displaying the data from certain columns in the table, something like this.
{% extends "base.html" %}
{% block content %}
{% for row in result %}
{{ row['content'] }}
{% endfor %}
{% endif %}
{% endblock %}
And literal HTML characters are being displayed.
You may just need to include the safe filter.
{{ row['content'] | safe }}
Did you find this article helpful?
If so, consider buying me a coffee over at