2013-10-15 30 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}} 

    },