目录

Flask - Static 文件

Web应用程序通常需要静态文件,例如javascript文件或支持网页显示的CSS文件。 通常,Web服务器配置为为您服务,但在开发过程中,这些文件是从包中的static文件夹或模块旁边提供的,它将在应用程序的/static提供。

特殊端点“static”用于生成静态文件的URL。

在下面的示例中,在index.html的HTML按钮的OnClick事件上调用hello.js定义的javascript函数,该函数在Flask应用程序的'/' URL上呈现。

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的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包含sayHello()函数。

function sayHello() {
   alert("Hello World")
}
↑回到顶部↑
WIKI教程 @2018