我在将字符串传递到 html 页面时遇到问题。具体在“< script>” block 中。我使用 Python、Flask、Jinja2 ...
在 python 中我有这样的东西:
def foo():
return myString #"[{title: 'Treino 7-Corrida',start: '2015-12-08',color: '#d00000',textColor: '#00c028'}]"
在 html 中我有这样的内容:
<html>
...
{{myString}} <!-- jinja2 -->
...
<script>
...
{{myString}} <!-- jinja2 -->
...
</script>
</html>
查看源代码我有这个:
<html>
...
[{title: 'Treino 7-Corrida',start: '2015-12-08',color: '#d00000',textColor: '#00c028'}]
...
<script>
...
[{title: 'Treino 7-Corrida',start: '2015-12-08',color: '#d00000',textColor: '#00c028'}]
...
</script>
</html>
问题是:
我做了什么,脚本不替换代码 '?
最佳答案
由于变量被转义,所以 jinja2 自动转义对 html 启用。
您应该尝试safe
过滤器:
Mark the value as safe which means that in an environment with automatic escaping enabled this variable will not be escaped.
因此将 html 编辑为以下内容:
{{ myString | safe }}
关于javascript - 使用 jinja2 将 Python 字符串转为 html javascript block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34156068/