为什么我的Django应用程序返回一个空白页面

时间:2016-04-22 11:12:49

标签: html django

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

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>
       
		<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>
&#13;
&#13;
&#13; 我的旧index.html

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分: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 Shift J)检查javascript错误

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

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