Jinja模板将双引号或单引号呈现为“”

时间:2012-01-25 16:01:56

标签: python jinja2

您好我正在尝试在jinja模板中填充Google Visualization API。 我拿了样本参数并将其传递给API 但它正在将单引号和双引号转换为'和& 这是脚本:

        <script type="text/javascript">
            //load the Google Visualization API and the chart
            google.load('visualization', '1', {'packages': ['columnchart']});

            //set callback
            google.setOnLoadCallback (createChart);

            //callback function
            function createChart() {

                //create data table object
                var dataTable = new google.visualization.DataTable();

                //define columns
                dataTable.addColumn('string','Quarters 2009');
                dataTable.addColumn('string', 'Earnings');

                //define rows of data
                // answerjson=answerjson.replace("&#39;",'"');
                {% set answerjson1='[["1": "Saturday"], ["6": "Sunday"], ["1": "Wednesday"], ["1": "Monday"], ["1": "Monday"], ["1": "Tuesday"], ["1": "Sunday"]' %}

                dataTable.addRows( {{answerjson1}} );

                //instantiate our chart object
                var chart = new google.visualization.ColumnChart (document.getElementById('chart'));

                //define options for visualization
                var options = {width: 400, height: 240, is3D: true, title: 'Company Earnings'};

                //draw our chart
                chart.draw(dataTable, options);

            }
</script>

这是传递给API的输入 I have put the screenshot because here it is rendered as double quotes

请帮助我,我需要做什么。

2 个答案:

答案 0 :(得分:19)

使用safe模板过滤器:

dataTable.addRows( {{ answerjson1 | safe }} );

答案 1 :(得分:1)

tojson过滤器以json格式呈现数据:

dataTable.addRows({{answerjson1 | tojson}});

如果您之后解析为json,安全过滤器可能会生成错误。例如,安全过滤器提供由单引号形成的json字符串,而JSON.parser()函数只能输入带双引号的字符串。