来自Django模板中JSON的Unescape html实体

时间:2013-10-24 15:47:49

标签: python google-app-engine django-templates html-entities

我从转发服务器获得响应:

'item':'<b> Some Data </b>'

我将此类数据传递给使用item= json.loads(response)

的模板

默认情况下,django模板(在Google App Engine中)进一步逃避,
所以它的双重逃脱了结果。 我可以使用safe删除一个级别的转义,如:

{{item|safe}}

如何将实体转换为相应的符号?

2 个答案:

答案 0 :(得分:4)

你可以这样做:

{% autoescape off %}
  {{ your_text_var }}
{% endautoescape %}

答案 1 :(得分:-2)

警告 - 这不是推荐的解决方案。您应该使用自动转换(检查Rafael's answer)。

以下应该做的工作 response.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>')

更新 - Jan Schär提出建议后,您应该使用以下内容: response.replace('&lt;', '<').replace('&gt;', '>').replace('&amp;', '&')

因为,如果response&amp;gt;,则会产生>而不是正确的&gt;。您应该在最后解析&amp;