当前位置:网站首页>Flask (VII) - static file

Flask (VII) - static file

2022-06-11 09:27:00 zju_ cbw

Web Applications often need a static file , For example, support the display of web pages JavaScript File or CSS file . Usually , Can be configured by Web The server provides these services , But in the development process , These files will be provided from a static folder or module in the package , It will be in the... Of the application /static The provision of .

Use special endpoints “ static state ” To generate... For static files URL.

In the following example ,index.html Medium HTML Button on the OnClick Event call hello.js As defined in javascript function , The function is in Flask Application's URL =>/ In .

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)

index.html Medium HTML The script is shown below .

<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>

file : hello.js Contains sayHello() function .

function sayHello() {
    
    alert("Hello World")
 }
原网站

版权声明
本文为[zju_ cbw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012301369566.html