gsingh2011 gsingh2011 - 11 months ago 50
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

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

Answer Source

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() %}
{% endfor %}