gpt4 book ai didi

html - 为什么我的 Django 应用程序返回空白页

转载 作者:可可西里 更新时间:2023-11-01 13:49:34 24 4
gpt4 key购买 nike

从 rasp.py 返回的值是:第一个是我连接到 raspberry 的传感器的数据,第二个是当前时间。这些值必须从我的 Django 应用程序 View 发送到我的 html 文件才能显示曲线,但结果是空白页。

树莓派

#!/usr/bin/env python
from datetime import datetime, timedelta
futuredate = datetime.now() + timedelta(days=10)
def foo ( ) :
i = 0
for i in range(0,19):
i += 1
tfile = open("/sys/bus/w1/devices/28-000007101990/w1_slave")
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temp = secondline.split(" ")[9]
temperature = float(temp[2:])
temperature = temperature/1000
mystr = str(temperature)
y = mystr.replace(",",".")
x = datetime.now() + timedelta(days=10)
return x,y

View .py

from django.shortcuts import render
from rasp import foo
import json
def index(request):
return render(request, 'index.html', {'t' : foo()})

index.html 脚本在html的头部

{% load staticfiles %}
<!DOCTYPE HTML>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Temperature sensor graph</title>
<!-- Core CSS - Include with every page -->
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">




<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
${demo.css}
</style>
<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 () {

series.addPoint([{{ t }}], 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(),
i;


data.push({ {{ t }} }
return data;
}())
}]
});
});
});
</script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
{% if user.is_authenticated %}
<a class="navbar-brand" href="/accueil">Accueil </a>
<a class="navbar-brand" href="/aps">Graphe </a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Account
<span class="caret"></span>
</a>

<ul class="dropdown-menu" role="menu">
<li>
<a href="/mail">changer mail</a>
</li>
<li>
<a href="#">changer temps</a>
</li>
<li class="divider"></li>
<li>
<a href="/logout">Logout</a>
</li>
</ul>
</li>
</ul>
</div>
</nav>

{% else %}

<a class="navbar-brand" href="/accueil">Accueil </a>
<a class="navbar-brand" href="/aps">Graphe </a>

<a class="navbar-brand" href="/login">S'authentifier </a>

</div>
</nav>
{% endif %}



<!-- Core Scripts - Include with every page -->
<script src = "{% static 'js/jquery.min.js' %}"></script>
<script src = "{% static 'js/bootstrap.min.js' %}"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

</body>
</html>
我的旧 index.html

<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(),
i;

for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: {{ t }}
});
}
return data;
}())
}]
});
});
});
</script>

最佳答案

首先,您需要独立于 View 测试您的foo() 函数。如果您不使用单元测试,从 django shell 调用 foo() 以确定它是否有效仍然是一件简单的事情。

如果您确实从命令中调用 foo(),您会发现它只返回一个元组。一个 x 和一个 y 值。这是你想要的吗?仅用一个点绘制曲线非常困难。

其次,在你的模板中

series.addPoint([{{ t }}], true, true);

相当于

series.addPoint([(10.1, 11.11)])

这是你真正想要的吗?进一步看,这将导致 javascript 语法错误

 data.push({ {{ t }}  }
return data;

括号(未闭合缺失)。您可以使用 chrome 开发人员控制台 (Ctrl Shift J) 检查 javascript 错误

请注意,在模板中遍历列表的正确方法是这样的:

{% for item in t %}
{{ item.x }}, {{ item.y }}
{% endfor %}

关于html - 为什么我的 Django 应用程序返回空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36792305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com