Flask静态文件

Flask教程 · 2019-04-25 19:16:38

Web应用程序通常需要一个静态文件,例如支持显示网页的JavaScript文件或CSS文件。 通常,可以通过配置Web服务器提供这些服务,但在开发过程中,这些文件将从包中的静态文件夹或模块旁边提供,它将在应用程序的/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")
}

点击查看所有 Flask教程 文章: https://www.codercto.com/courses/l/47.html

查看所有标签

The Intersectional Internet

The Intersectional Internet

Safiya Umoja Noble、Brendesha M. Tynes / Peter Lang Publishing / 2016

From race, sex, class, and culture, the multidisciplinary field of Internet studies needs theoretical and methodological approaches that allow us to question the organization of social relations that ......一起来看看 《The Intersectional Internet》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试