Home  >  Article  >  Web Front-end  >  django中,kindeditor存到数据库的html,到了前台被html标签被自动转义的解决办法_html/css_WEB-ITnose

django中,kindeditor存到数据库的html,到了前台被html标签被自动转义的解决办法_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-21 09:09:541550browse

1,使用kindeditor进行了上传图片功能,存储到后台的html代码为:

<img src="/static/content_img/img_2015-07-21-024421.jpg" alt="" />KindEditor

2,我把这个unicode字符串返回到前台的模板,结果显示了html代码:

<img src="/static/content_img/img_2015-07-21-024421.jpg" alt="" />KindEditor

3,自己开始的解决办法:

   存的时候进行escape

content = cgi.escape(content)

   这样处理后存到后台的代码变成了:

<img src="/static/content_img/img_2015-07-21-024421.jpg" alt="" />KindEditor

   取的时候unescape一下

import HTMLParserhtml_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回到页面,结果显示正常。

   又查了下自己原来处理方式的前台的源代码,结果是被转义后的。于是想到如果通过{{content}}方式在前台显示html代码

   的话,django模板在编译的时候,会自动对html标签进行转义,稍微查了下,使用以下方式不让django模板自动转义

   html标签。同时也解决了自己的问题。

{% autoescape off %}{{infoContent}}{% endautoescape %}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn