允许< br> Google App Engine和Jinja2的标签 [英] Allowing <br> tags with Google App Engine and Jinja2

查看:118
本文介绍了允许< br> Google App Engine和Jinja2的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网络应用程序中,用户可以创建博客帖子。当我显示博客文章时,换行不会显示,因为我没有用< br> 标签替换新行。问题是我已经在Jinja中自动转义,所以< br> 标签被转义。我不想临时禁用自动转义,我想要具体允许< br> 标签。我该怎么做?

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?

推荐答案

我有另一个答案,我认为是最好的。最初我只是按照原样显示我的变量 post.content ,并且换行没有被保留。这里的任何一个解决方案都不是很好,而且我的解决方案只是一个快速的解决方案,并且有重大的问题。这是真正的解决方案:

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 %}

这篇关于允许&lt; br&gt; Google App Engine和Jinja2的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆