2017-05-26 91 views
1

我在后台使用bottle和peewee作为框架,sqlite3作为数据库。 summernote是前面的文本编辑器。成功将代码保存到数据库中,但在从数据库中检索时未能显示文本。Summernote无法转换html代码

DB数据: The Draft column is the html

源代码:

前:

$('#summernote').summernote("code", "{{content}}"); 

后端:

template('apps_post_editer', appName='Post New', pid=newPost.id, title=('' if newPost.title is None else str(newPost.title)), content=('' if newPost.draft is None else unicode(str(newPost.draft), "utf-8"))) 

我认为这是开始时的编码问题,所以我使用unicode来打开utf-8中的值,但不起作用。而且也没有只str(newPost.draft)

结果: You can see that the html code is not converted

问: 为什么会发生这样的?有没有解决方法?

非常感谢。

更新:对不起,这是我的第一个问题,不知道为什么图片不显示...请点击链接以获得更多的细节...... OK ...需要10声誉

回答

1

当你想用瓶子渲染来自数据库的HTML时,你必须告诉渲染引擎该内容是安全渲染的,以避免XSS攻击。

随着布特尔您可以禁用转义像这样的表达式:

{{! summernotecontent}} 
你的情况

这将是:

$('#summernote').summernote("code", "{{! content}}"); 

你可以找到关于此主题的文档中瓶here

+0

哦,是的!它的工作原理,只需要对$('#summernote')进行一些修改。summernote('code','{{!content}}');由于Summernote中的html标签属性的值被保存在双引号中,因此需要删除!和内容之间的空白,最后,非常感谢您的帮助! – snsyzb