2016-04-22 103 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

相關問題