我使用的是Flask和Sqlite。
我获取一些包含换行符的字符串,并将其存储在数据库中。稍后,我从db获取它,并将其包含在某个页面上,字符串显示时没有换行符。那是什么?
例如,如果我有
{{ entry.content }}
in my template, and the entry that was stored had content "hello\nhello", it displays "hellohello" on the page.但是,如果我有
{{ entry.content.replace('\r\n','') }}
or{{ entry.content.replace('\r\n',‘ ') }}
in my template, it will display "hello
hello" or "hello页面上的“hello”。
所以我的印象是换行符没有被浏览器解释和显示。我做错了什么?
发布于 2012-12-23 11:38:05
尝试{{ entry.content|safe }},这样Flask/Jinja就不会逃脱你的超文本标记语言。
(不过要小心,因为任何用户输入的内容,包括脚本标记,都将按原样输出。如果您真的想保持谨慎,并且只允许
你可能想写你自己的擦洗器:Jinja2 escape all HTML but img, b, etc)
https://stackoverflow.com/questions/14008300
复制相似问题