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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Note that i have autoescape on by default, so I don't check it in this function, but this is what I'm using
then of course,
The easiest way to do this is to escape the field yourself, then add line breaks. When you pass it in in jinja, mark it as safe so it's not autoescaped.
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:The solution was to put
<pre></pre>
tags around the area where I had the content.You can use the
|safe
filter, or use the autoescape blocks:You could also set autoescaping in the environment to
False
.In your model object, add a function like this:
Then in your template, just call that: