将django后台数组搬到js前台

            // JS 代码 
            var chart = Highcharts.chart('container', {
                chart: {
                        plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false
                },
                title: {
                        text: '扇区突出演示'
                },
                tooltip: {
                        headerFormat: '{series.name}<br>',
                        pointFormat: '{point.name}: <b>{point.percentage:.1f}%</b>'
                },
                plotOptions: {
                        pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                        enabled: true,
                                        format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                                        style: {
                                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                        }
                                },
                                states: {
                                        hover: {
                                                enabled: false
                                        }  
                                },
                                slicedOffset: 20,         // 突出间距
                                point: {                  // 每个扇区是数据点对象,所以事件应该写在 point 下面
                                        events: {
                                                // 鼠标滑过是,突出当前扇区
                                                mouseOver: function() {
                                                        this.slice();
                                                },
                                                // 鼠标移出时,收回突出显示
                                                mouseOut: function() {
                                                        this.slice();
                                                },
                                                // 默认是点击突出,这里屏蔽掉
                                                click: function() {
                                                        console.log(this);
                                                        return false;
                                                }
                                        }
                                }
                        }
                },
                series: [{
                        type: 'pie',
                        name: '区县占比',
                        data: {{ array1 | safe }}
                }]
            });

最后一句中:

{{ array1 | safe }}

从这里来:

def piedata(request):
    array1 = [['通化县', 1], ['集安', 1], ['梅河', 2], ['柳河', 3], ['城区', 9]]
    context = {'array1': array1} # context数据字典中的key——'array1'就是传递给js的array1 
    return render(request, "pie_chart.html", context)

 

context数据字典中的key——'array1'就是传递给js的array1,safe表示不转码,避免将引号转为转义字符。

https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial03/#write-views-that-actually-do-something

官方文档例子不是很明显。是在循环中传递的数组。

{% for question in latest_question_list %}

 

 

https://blog.csdn.net/watermelonbig/article/details/53946218

为什么需要使用{{ fileuri_json|safe }}这样的形式?
Django的模板中会对HTML标签和JS等语法标签进行自动转义,原因显而易见,这样是为了安全。然后上面的"|safe"的作用正是关闭Django的自动转义功能。
如果不做这个干预,可以运行下面代码观察下效果:
<script>
    alert('{{ fileuri_json }}')
</script>
这是页面弹出的窗口内容:
[&quot;/data/server/config/utf8.users&quot;, &quot;/data/server/config/important.properties&quot;]

可以看到,Django把fileuri_json中的双引号全部转义了。
如果拿这个被转义后的字符串交给页面js去当数组处理,肯定是无法实现的。
--------------------- 
作者:运维个西瓜 
来源:CSDN 
原文:https://blog.csdn.net/watermelonbig/article/details/53946218?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值