2016-05-16 83 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)是靜態的夾。