English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
웹 애플리케이션은 일반적으로 정적 파일이 필요합니다. 예를 들어, 웹 페이지를 표시하는 JavaScript 파일이나 CSS 파일입니다. 일반적으로 이러한 서비스는 웹 서버를 통해 제공됩니다만, 개발 과정에서 이 파일들은 패키지의 정적 폴더나 모듈 옆에서 제공됩니다. 이는 애플리케이션의/static에서 제공됩니다。
특별한 엔드포인트 "static"을 사용하여 정적 파일에 URL을 생성합니다。
다음 예제에서 index.html에 있는 HTML 버튼의 OnClick 이벤트는 hello.js에 정의된 javascript 함수를 호출합니다. 이 함수는 Flask 애플리케이션의 URL => / 에서 표시됩니다.
# 파일 이름: example.py # 저작권: 2020 By w3codebox # 저자: ko.oldtoolbag.com # 날짜: 2020-08-08 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 스크립트는 다음과 같습니다.
# 파일 이름: example.py # 저작권: 2020 By w3codebox # 저자: ko.oldtoolbag.com # 날짜: 2020-08-08 <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() 함수를 포함한 정의.
# 파일 이름: example.py # 저작권: 2020 By w3codebox # 저자: ko.oldtoolbag.com # 날짜: 2020-08-08 function sayHello() { alert("Hello World") }