[英]Write JS variable containing “</script>” string into Django HTML template
How would I write the following Python object to a JavaScript variable in a template: 如何将以下Python对象写入模板中的JavaScript变量:
{"foo": {"bar": "</script>"}, "baz": 1}
The above object would be written to a JS variable in an HTML template. 上面的对象将被写入HTML模板中的JS变量。 That variable would then have two properties, foo
and baz
. 该变量将具有两个属性foo
和baz
。 The foo
property would evaluate to an object having a property bar
. foo
属性将评估为具有属性bar
的对象。 A problem is that the </script>
tag closes the parent <script>
element (in the HTML template) prematurely. 问题在于</script>
标记过早关闭了父<script>
元素(在HTML模板中)。
Thanks to spectras. 多亏了光谱。
In Python (2.7): 在Python(2.7)中:
js = json.dumps({"foo": {"bar": "</script>", "url":"one/two/three"}, "baz": 1})
In the Django (1.7) template: 在Django(1.7)模板中:
<script>
var data = JSON.parse('{{ js|escapejs }}');
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.