1,使用kindeditor进行了上传图片功能,存储到后台的html代码为:html
<img src="/static/content_img/img_2015-07-21-024421.jpg" alt="" />KindEditor
2,我把这个unicode字符串返回到前台的模板,结果显示了html代码:python
<img src="/static/content_img/img_2015-07-21-024421.jpg" alt="" />KindEditor
3,本身开始的解决办法:
django
存的时候进行escape测试
content = cgi.escape(content)
这样处理后存到后台的代码变成了:code
<img src="/static/content_img/img_2015-07-21-024421.jpg" alt="" />KindEditor
取的时候unescape一下htm
import HTMLParser html_parser = HTMLParser.HTMLParser() infoContent = html_parser.unescape(info.content)
这样处理后的代码变成了:图片
<img src="/static/content_img/img_2015-07-21-024421.jpg" alt="" />KindEditor
4,可是这样问题没有获得处理,因而本身写了一个测试,直接把这段html字符串HttpResponse回到页面,结果显示正常。unicode
又查了下本身原来处理方式的前台的源代码,结果是被转义后的。因而想到若是经过{{content}}方式在前台显示html代码字符串
的话,django模板在编译的时候,会自动对html标签进行转义,稍微查了下,使用如下方式不让django模板自动转义it
html标签。同时也解决了本身的问题。
{% autoescape off %} {{infoContent}} {% endautoescape %}