gsingh2011 gsingh2011 - 1 month ago 6
Python Question

Allowing <br> tags with Google App Engine and Jinja2

In my web app, the user can make blog posts. When I display the blog post, newlines aren't shown because I didn't replace the new lines with

<br>
tags. The problem is that I've turned autoescaping on in Jinja, so
<br>
tags are escaped. I don't want to temporarily disable autoescaping, I want to specifically allow
<br>
tags. How would I do this?

Answer

I have another answer that I think is the best. Initially I was just displaying my variable post.content as-is, and the newlines weren't being preserved. None of the solutions here worked (well), and my pre solution was just a quick fix and had major issues. This is the real solution:

{% for line in post.content.splitlines() %}
    {{line}}<br>
{% endfor %}