flask 連結 url_for()

來源:互聯網
上載者:User

標籤: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()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.