Flask--靜態資源


靜態資源

from flask import Flask, render_template

app = Flask(__name__, template_folder="templates", static_folder="static", static_url_path="/static")

# template_folder:指定HTML文件查找目錄
# static_folder:指定靜態資源存放目錄
# static_url_path:HTML中靜態資源的標志

# 裝飾器形式
# @app.route("/index")
# def index():
#     return render_template("index.html")

# 非裝飾器形式
def index():
    return render_template('index.html')
app.add_url_rule('/index', 'index', index)

if __name__ == '__main__':
    app.run()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
## 引用靜態資源的兩種方法,推薦第二種
<img src="/static/01.jpg"/>
  
<img src="{{ url_for('static',filename='01.jpg')}}" />
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM