Django模板包含{%for%}和{%if%}
Django Template with {% for %} and {% if %}?
我已经在视图中声明了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 = ['23.125', '23.125', '23.125', '23.125', '23.125', '23.125', '23.125', '23.125', '23.125', '23.187', '23.125', '23.187'] ;
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模板中使用变量作为索引?
相关文章:
- for循环中的javascript if语句找不到==
- for 循环和 if 语句,获取变量并存储它
- 使用javascript中的for循环和if语句
- for循环与if条件
- Django模板包含{%for%}和{%if%}
- 在 for 循环中使用 if 语句
- 识别两个文本中的单词(if 语句在 for 循环中)
- for 循环中的 if 语句
- for 循环中的 if 语句都执行,而不仅仅是一个
- if数组元素在for循环内部有条件
- for循环,if else不工作Javascript
- for循环内部If语句的JavaScript正确语法
- WordPress - Enqueue scripts for only if LT IE 9
- 使用cookie(javascript)时,for循环中的If/Elseif语句出现问题
- for循环中嵌套if条件的setTimeout
- 在 for 之后的 if 语句中使用对象项
- 通过在 IF 语句和 FOR 循环两边加上括号,函数变得有问题
- If-else for value javascript
- Creating if for dblclick td
- Jscript if for全局变量