2016-11-22 53 views
0

我使用Flask-JSGlue,它應該在JavaScript中爲Flask對象提供類似url_for的方法。我在scripts.js得到Uncaught ReferenceError: Flask is not defined。爲什麼我得到這個錯誤?Flask-JSGlue不向JavaScript文件提供Flask對象

from flask_jsglue import JSGlue 

app = Flask(__name__) 
JSGlue(app) 

@app.route("/city") 
def city(): 
    return render_template("city.html") 

scripts.js

$(document).ready(function() { 
    if (location.pathname == '/city') { 
     $.getJSON(Flask.url_for('city'), parameters).done(function (json_city, x, y) { 
      document.body.style.backgroundImage = json_city['background_filename']; 
     }); 
    } 
}); 

回答

0

As the docs explain,你需要包括您呈現的頁面上的燒瓶JSGlue的腳本。你需要在之前包含它

<head> 
    {{ JSGlue.include() }} 
    <script src="{{ url_for('static', filename='script.js') }}"></script> 
</head> 
-1

你需要先導入瓶:

from flask import Flask