为什么我的Django项目不起作用?

时间:2016-03-03 11:25:58

标签: django

我有一个简单的Django项目,我必须在其中显示点图。我遇到了两个问题:首先是我在Windows上的html文件正常工作,但在ubuntu上它只显示页面的头部。 secoond问题是当我运行服务器时,即使html fole为空我也找不到页面404。这些是我处理过的文件,并打印了网络结果的屏幕:

模板/ APS / index.html中

<!DOCTYPE html>
<html>
    <head>
      <meta http-equip="refresh" content ="3">
    </head>
 <body>

  <h3>Valeurs enregistrés par notre capteur de temperature<h3> 



 <canvas id="graph"></canvas>
 <script>
  var graph;
  var xPadding = 40;
  var yPadding = 30;

        // Notice I changed The X values

             var data = { values:[
               { X: 0, Y: 16 },
               { X: 3, Y: 28 },
               { X: 6, Y: 18 },
               { X: 9, Y: 34 },
               { X: 12, Y: 40 },
               { X: 15, Y: 80 },
               { X: 18, Y: 60 }
            ]};

            // Returns the max Y value in our data list
            function getMaxY() {
                var max = 0;

                for(var i = 0; i < data.values.length; i ++) {
                    if(data.values[i].Y > max) {
                        max = data.values[i].Y;
                    }
                }

                max += 10 - max % 10;
                return max;
            }

            // Returns the max X value in our data list
            function getMaxX() {
                var max = 0;

                for(var i = 0; i < data.values.length; i ++) {
                    if(data.values[i].X > max) {
                        max = data.values[i].X;
                    }
                }

                max += 10 - max % 10;
                return max;
            }

            // Return the x pixel for a graph point
            function getXPixel(val) {
                // uses the getMaxX() function
                return ((graph.width - xPadding) / getMaxX()) * val + (xPadding * 1.5);
            }

            // Return the y pixel for a graph point
            function getYPixel(val) {
                return graph.height - (((graph.height - yPadding) / getMaxY()) * val) - yPadding;
            }

                graph = document.getElementById("graph");
                var c = graph.getContext('2d');            

                c.lineWidth = 2;
                c.strokeStyle = '#333';
                c.font = 'italic 8pt sans-serif';
                c.textAlign = "center";

                // Draw the axises
                c.beginPath();
                c.moveTo(xPadding, 0);
                c.lineTo(xPadding, graph.height - yPadding);
                c.lineTo(graph.width, graph.height - yPadding);
                c.stroke();

                // Draw the X value texts
                for(var i = 0; i < data.values.length; i ++) {
                    // uses data.values[i].X
                    c.fillText(data.values[i].X, getXPixel(data.values[i].X), graph.height - yPadding + 20);
                }

                // Draw the Y value texts
                c.textAlign = "right"
                c.textBaseline = "middle";

                for(var i = 0; i < getMaxY(); i += 10) {
                    c.fillText(i, xPadding - 10, getYPixel(i));
                }

                c.strokeStyle = '#f00';

                // Draw the line graph
                c.beginPath();
                c.moveTo(getXPixel(data.values[0].X), getYPixel(data.values[0].Y));
                for(var i = 1; i < data.values.length; i ++) {
                   // c.lineTo(getXPixel(data.values[i].X), getYPixel(data.values[i].Y));
                }
                c.stroke();

                // Draw the dots
                c.fillStyle = '#333';

                for(var i = 0; i < data.values.length; i ++) {  
                    c.beginPath();
                    c.arc(getXPixel(data.values[i].X), getYPixel(data.values[i].Y), 4, 0, Math.PI * 2, true);
                    c.fill();
                }
</script> 

</body>
</html>

view.py

from django.shortcuts import render

def index(request):
context = {}
return render(request, 'aps/index.html', context)

newsite / APS / url.py

from django.conf.urls import url

from . import views

 urlpatterns = [
 url(r'^$', views.index, name='index')
 ]

newsite / urls.py

from django.conf.urls import include, url
from django.contrib import admin
from aps import views

urlpatterns = [
url(r'^admin/', admin.site.urls),    
url(r'^aps/', include('aps.urls')), 
]

http://127.0.0.1:8000/

3 个答案:

答案 0 :(得分:0)

您的索引页面位于/ aps /,而不是/.

但是我不确定你为什么要使用Django,因为你所做的只是提供带有一些Javascript的静态HTML页面。

答案 1 :(得分:0)

如果您

,则需要在网址中添加额外内容
from django.conf.urls import include, url
from django.contrib import admin
from aps import views

urlpatterns = [
url(r'^/', include('aps.urls')), 
url(r'^admin/', admin.site.urls),    
url(r'^aps/', include('aps.urls')), 
]
这样你就可以在你的入口路径上找到一些东西,它不会崩溃。 为了避免崩溃的页面(但现在不是这样),你也应该在你的设置中        Debug=False

答案 2 :(得分:0)

感谢您的回答,问题出在views.py中,应该是

from django.shortcuts import render
def index(request):
  return render(request, 'aps/index.html', {})