繁体   English   中英

带有{%for%}和{%if%}的Django模板?

[英]Django Template with {% for %} and {% if %}?

我已经在我的观点中宣布了这一点。 当我运行我的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循环

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM