【问题标题】:Single Quotes not showing up properly in HighCharts graph单引号未在 HighCharts 图表中正确显示
【发布时间】:2013-10-22 19:04:31
【问题描述】:

在我的 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'

【问题讨论】:

    标签: javascript django highcharts django-templates


    【解决方案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 }}
    

    【讨论】:

      【解决方案2】:

      尝试使用escapejsescape 过滤器。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-09
        • 1970-01-01
        • 1970-01-01
        • 2022-10-14
        • 2015-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多