-1

I am using Flask as the backend and reading a piece of text from a file that contains newlines as \n. I wish to render this text inside a div. To do so, I am replacing the \n with <br> and then passing it to the render_template() method.

@app.route('/')
@app.route('/index')
def index():
        content = read_file('filename.csv') 
        content = content.replace('\\n','<br>')
        return render_tempate('index.html',text=content)

However, doing so shows the br tag as text. The underlying html shows that the br tag is being interpreted as &lt;br&gt;

<div>some text &lt;br&gt; some more text</br>

How can I fix this and render a newline inside the div?

Jake Conway
  • 901
  • 16
  • 25

1 Answers1

4

In index.html, change your {{text}} tags to {{text|safe}}. By default, HTML strings are escaped in render_template, and the |safe is needed to turn that off.

Jake Conway
  • 901
  • 16
  • 25