標籤:charset eve 代碼 click 靜態檔案 html oca block ext
通常html的檔案都放在template裡面,那麼靜態檔案放在哪呢?staitc裡面
調用 url_for(‘static‘, filename=‘css/styles.css‘, _external=True) 得到的結果是http:// localhost:5000/static/css/styles.css。
預設設定下,Flask 在程式根目錄中名為 static 的子目錄中尋找靜態檔案。如果需要,可在 static 檔案夾中使用子檔案夾存放檔案。
伺服器收到前面那個 URL 後,會產生一個響應, 包含檔案系統中 static/css/styles.css 檔案的內容。
請看案例:
{% block head %}{{ super() }}<link rel="shortcut icon" href="{{ url_for(‘static‘, filename = ‘favicon.ico‘) }}"type="image/x-icon"><link rel="icon" href="{{ url_for(‘static‘, filename = ‘favicon.ico‘) }}"type="image/x-icon">{% endblock %}
案例:
在看代碼
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><h1>hello, {{ name }}</h1><img src="{{ url_for(‘static‘, filename=‘asd.jpg‘) }}"></body></html>靜態
from flask import Flask, render_templateapp = Flask(__name__)@app.route(‘/‘)def index(): name = "<h1>Hello</h1>" return render_template(‘index.html‘, name=name)@app.errorhandler(404)def url_error(e): return render_template(‘index.html‘, name=404), 404@app.errorhandler(500)def url_error2(e): return render_template(‘500.html‘), 500if __name__ == ‘__main__‘: app.run(debug=True)
run.py
flask 連結 url_for()