2016-04-22 69 views
3

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

rasp.py

#!/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 

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

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

 
\t </body> 
 
</html>
我的老指数的头部。 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; 
 
       }()) 
 
      }] 
 
     }); 
 
    }); 
 
}); 
 
\t \t </script>
所有的

回答

1

首先,你需要测试你的独立观点的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移十)

注意,通过在模板列表进行迭代的正确方法是这样的JavaScript错误:

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

请看看我的编辑问题我已经添加了我的旧html文件,我的for循环是在html中,而不是在rasp.py中,并且它呈现相同的数据,它返回一条线而不是曲线,但是当我更改它时。它返回一个空白页面 – bne

+0

已更新,回答。旧的html似乎没有语法错误。但是你的rasp.py仍然只返回一个元组而不是元组列表。 – e4c5

+0

是的,我不知道如何每3秒从锉刀发送数据到我的html,以便从我的传感器获取不同的数据 – bne

相关问题