Un endpoint speciale 'statico' viene utilizzato per generare l'URL per i file statici.
Nell'esempio seguente, una funzione javascript definita in hello.js viene richiamata sull'evento OnClick del pulsante HTML in index.html , che viene visualizzato nell'URL "/" dell'applicazione Flask.
from flask import Flask, render_template app = Flask(__name__) @app.route("/") def index(): return render_template("index.html") if __name__ == '__main__': app.run(debug = True)Di seguito è riportato lo script HTML di index.html .
<html> <head> <script type = "text/javascript" src = "{{ url_for('static', filename = 'hello.js') }}" ></script> </head> <body> <input type = "button" onclick = "sayHello()" value = "Say Hello" /> </body> </html>hello.js contiene la funzione sayHello () .
function sayHello() { alert("Hello World") }
No comments:
Post a Comment