2013-10-15 27 views
1

在我的Django的模板,我有以下代码:单引号不能正确显示出来的HighCharts图

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' 

回答

1

这里

https://docs.djangoproject.com/en/1.1/topics/templates/

见它说

默认情况下在Django,每个模板自动转义每一个变量标签的输出。具体来说,这五个字符转义:

< is converted to &lt; 
> is converted to &gt; 
' (single quote) is converted to &#39; 
" (double quote) is converted to &quot; 
& is converted to &amp; 

对于各个变量

要禁用自动转义为单个变量,使用安全过滤器:

This will be escaped: {{ data }} 
This will not be escaped: {{ data|safe }} 
1

试着用escapejsescape过滤器。

{% for item in graph_data %} 
    { 
     name: "{{item|escapejs}}", 
     x: Date.UTC({{item.date.year}},{{item.date.month}},{{item.date.day}}), 
     y: {{item.rating}} 

    },