Search code examples
pythonflaskline-breaks

Flask: render_template context containing line breaks


In my flask application, I have the situation where the page I will load will contain information from the form of the previous page. Here is some example code:

s = ""
for row in cursor:
    s += "Name: " + row[0] + "<br/>Age: " + row[1] + "<br/>"
return render_template("name.html", ID=s)

The only problem is that the html tags appear as plain text and do not get rendered into the html. Is there a way for the <br/> to be translated as line breaks? I've tried using \n instead but it does not work.

Am I approaching this the wrong way? Does this functionality already exist elsewhere?


Solution

  • Shouldn't you just pass the data to the template and loop in the template?

    return render_template("name.html", rows=cursor)
    

    In the template:

    {% for row in rows %}
        Name: {{row.0}}<br>Age: {{row.1}}<br>
    {% endfor %}