靜態文件
靜態文件通常包含CSS和JavaScript文件,開發期間,Flask只需在包或模塊旁邊創建一個static文件夾,它就可以在/static應用程序中找到。
注意:生產環境中,Flask關閉調試模式後加載不了靜態資源,應通過服務器正向代理靜態資源
靜態文件加載 {{ url_for(‘static’, filename=”靜態文件路徑”) }}
如: url_for(‘static’, filename=’style.css’)
該文件必須存儲在文件系統中static/style.css。
渲染模板
通過使用render_template() 方法來渲染模板,示例如下:
from flask import render_template
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html')
Flask將在templates文件夾中查找模板。因此,如果您的應用程序是一個模塊,那麼該文件夾就在該模塊的旁邊,如果它是一個包,它實際上在您的包中:
案例1:一個模塊:
/application.py
/templates
/hello.html
案例2:一個包:
/application
/__init__.py
/templates
/hello.html