Django模板包含{%for%}和{%if%}

Django Template with {% for %} and {% if %}?

本文关键字:%if% %for% 包含 Django      更新时间:2023-09-26

我已经在视图中声明了t。当我运行django应用程序时,它显示一个空白页面,但当我按下ctrl+u时,我可以看到我的y值被正确呈现,但我的x值被呈现为空白。我的目标是在x中显示当前时间。

<script type="text/javascript">
$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });
        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function () {
                        // set up the updating of the chart each second
                       var series = this.series[0];
                        setInterval(function () {
                            var x = (new Date()).getTime(), // current time
                                y =  {{ t }} ;
                            series.addPoint([x, y], true, true);
                        }, 3000);
                    }
                }
            },
            title: {
                text: 'Live temperature sensor values'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br/>' +
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Sensor data',
                data: (function () {
                    // generate an array of sensor data
                    var data = [],
                        time = (new Date()).getTime(),

                      {% for item in t %}
                        data.push({
                         {% for i in 12 %}
                          {% if  t.i  == item %}
                            x: time + {{ i }} * 3000,
                          {% endif %}    
                         {% endfor %}
                            y: {{ item }}
                        });
                    {% endfor %}
                    return data;
                }())
            }]
        });
    });
});
        </script>

ctrl+u

<script type="text/javascript">
$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });
        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function () {
                        // set up the updating of the chart each second
                       var series = this.series[0];
                        setInterval(function () {
                            var x = (new Date()).getTime(), // current time
                                y =  [&#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.187&#39;, &#39;23.125&#39;, &#39;23.187&#39;] ;
                            series.addPoint([x, y], true, true);
                        }, 3000);
                    }
                }
            },
            title: {
                text: 'Live temperature sensor values'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br/>' +
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Sensor data',
                data: (function () {
                    // generate an array of sensor data
                    var data = [],
                        time = (new Date()).getTime(),
                     
                      
                        data.push({
                            x: time + 1 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 2 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 3 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 4 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 5 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 6 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 7 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 8 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 9 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 10 * 3000,
                            y: 23.187
                        });
                    
                        data.push({
                            x: time + 11 * 3000,
                            y: 23.125
                        });
                    
                        data.push({
                            x: time + 12 * 3000,
                            y: 23.187
                        });
                    
                    return data;
                }())
            }]
        });
    });
});
		</script>

我猜一下你想做什么……你试图在列表上迭代,当你找到一个等于项的值时,你想显示一个x值,这看起来相当于只添加item作为x

{% for item in t %}
  data.push({ x: time + {{ item }} * 3000,

当物品处于的第二位置时,时间+1*1000

你可以使用forloop.counter进行

  data.push({ x: time + {{ forloop.counter }} * 3000,

至于为什么你不能做另外两件事的参考:

遍历一个范围-Django模板中的Numeric for循环

通过变量访问数组元素-如何在django模板中使用变量作为索引?