Jas*_*ask 4 python google-app-engine django-templates html-entities
我从被转义的服务器收到响应:
'item':'<b> Some Data </b>'
Run Code Online (Sandbox Code Playgroud)
我将这些数据传递给模板使用 item= json.loads(response)
默认情况下,django 模板(在 Google App Engine 中)会进一步转义它,
因此它在结果中被双重转义。我可以safe用来删除一层转义,如:
{{item|safe}}
Run Code Online (Sandbox Code Playgroud)
我如何将实体转换为相应的符号?
你可以这样做:
{% autoescape off %}
{{ your_text_var }}
{% endautoescape %}
Run Code Online (Sandbox Code Playgroud)
sha*_*k3r -3
警告- 这不是推荐的解决方案。您应该使用自动转义(检查拉斐尔的答案)。
\n\n以下应该可以完成这项工作。
\nresponse.replace(\'&\', \'&\').replace(\'<\', \'<\').replace(\'>\', \'>\')
更新 - \n根据Jan Sch\xc3\xa4r的建议,您应该使用以下内容:\nresponse.replace(\'<\', \'<\').replace(\'>\', \'>\').replace(\'&\', \'&\')
因为,如果response是&gt;,它将导致>而不是正确的>。你应该&在最后解决。