在我的Django模板中,我有以下代码:
series: [{
name: 'Ratings',
data: [
{% for item in graph_data %}
{
name: "{{item}}",
x: Date.UTC({{item.date.year}},{{item.date.month}},{{item.date.day}}),
y: {{item.rating}}
},
{% endfor %}
]
}]
但是,当名称中包含单引号时,例如:
The Story Behind 'Toy Story'
在图表上,它显示为:
The Story Behind %#39;Toy Story'
答案 0 :(得分:1)
尝试使用escapejs
或escape
过滤器。
{% for item in graph_data %}
{
name: "{{item|escapejs}}",
x: Date.UTC({{item.date.year}},{{item.date.month}},{{item.date.day}}),
y: {{item.rating}}
},
答案 1 :(得分:1)
见这里
https://docs.djangoproject.com/en/1.1/topics/templates/
它说
默认情况下,Django中的每个模板都会自动转义每个变量标记的输出。具体来说,这五个字符被转义:
< is converted to <
> is converted to >
' (single quote) is converted to '
" (double quote) is converted to "
& is converted to &
对于个别变量
要禁用单个变量的自动转义,请使用安全过滤器:
This will be escaped: {{ data }}
This will not be escaped: {{ data|safe }}