2016-05-16 49 views
0

我想在我的网站上使用processing.js但我不断收到错误:“processing.min.js无法加载资源:服务器与404(未找到)状态回答”为什么我无法使用Flask加载Processing.js草图?

我用瓶的render_template()在test.html文件加载:

<!doctype html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Processing.js</title> 
    <script src="processing.min.js"></script> 
    </head> 
    <body> 
    <h1>Processing.js</h1> 
    <script type="text/processing"> 
    void setup() { 
     size(400,200); 
     textAlign(CENTER, CENTER); 
     background(0,0,100); 
     fill(255,230,75); 
     text("Processing.js", width/2, height/2); 
     noLoop(); 
     } 

     void draw() { 
     } 

     void mouseMoved() { 
     stroke(255); 
     point(mouseX, mouseY); 
     redraw(); 
     } 

     void mousePressed() { 
     line(0,mouseY,width,mouseY); 
     line(mouseX,0,mouseX,height); 
     println(mouseX, mouseY); 
     } 
    </script> 
    <canvas></canvas> 
    </body> 
</html> 

烧瓶文件,我再来看很简单:

from flask import Flask,request,render_template 
app = Flask(__name__) 

@app.route("/") 
def hello(): 

    return render_template('test.html') 

if __name__ == '__main__': 
    app.debug = True 
    app.run() 

当我在本地主机上运行时:http://127.0.0.1:5000/我看到标头Processing.js但没有实际的画布元素。

+0

鉴于所有Flask正在做的是提供两个文件,如果您在没有Flask的情况下提供文件,这是否工作?另外,这不是你如何链接到静态文件,你可以使用'url_for('static',filename ='processing.min.js')'。 – davidism

+0

嘿@davidism,你没有烧瓶是什么意思?我是新手,我只有Flask作为后端框架的经验。 – sam

+0

我仍然收到这404个未找到的消息。我在我的“静态”文件夹中有processing.js,“模板”文件夹中有test.html模板,外部有main.py烧瓶文件。 GET http://127.0.0.1:5000/processing.js GET http://127.0.0.1:5000/url_for('static',%20filename='processing.js')404未找到 – sam

回答

0

根据瓶docs

Dynamic web applications also need static files. That’s usually where the CSS and JavaScript files are coming from. Ideally your web server is configured to serve them for you, but during development Flask can do that as well. Just create a folder called static in your package or next to your module and it will be available at /static on the application.

而作为davidism评论,请使用url_for模板方法来引用文件

<script type="text/javascript" src="url_for('static', filename='processing.js')"></script> 

假设您的静态文件(的.js和.css)是静态的夹。