来源:https://sentry.io/answers/serve-static-files-flask/
语法
from flask import url_for
url_for('static', filename='css/app.css')
如果是 bluprint 中的 path ,需要把 blueprint 的 name 也加上。
app = Blueprint('certificate', __name__, url_prefix='/')
@app.route("/api/add", methods=['POST'])
def api_add_task():
return redirect(url_for('certificate.api_add_task'), code=302)
目录结构
app/app.py
app/static/css/app.css
app/templates/index.html
参数
来源:https://stackoverflow.com/questions/7478366/create-dynamic-urls-in-flask-with-url-for
路由定义
@app.route('/<variable>/add', methods=['GET', 'POST'])
def add(variable):
@app.route('/<variable>/remove', methods=['GET', 'POST'])
def remove(variable):
生成 url
url_for('add', variable=foo)
url_for('remove', variable=foo)
例子
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="{{url_for('static', filename='css/style.css')}}">
<title></title>
</head>
<body>
<img src="{{url_for('static', filename='img/myimage.png')}}">
</body>
</html>