fastapi:模板报错:starlette.routing.NoMatchFound: No route exists for name "static" and params "filename".
一,代码和报错信息:
代码:
<img class="mb-4 rounded-circle" src="{{ url_for('static', path='image/logo/logo_300.jpg') }}" alt="" width="100">
报错信息
starlette.routing.NoMatchFound: No route exists for name "static" and params "filename".
二,原因
很多开发者是从 Flask 转到 FastAPI 的,容易惯性地写成 filename。
-
Flask 的写法:
url_for('static', filename='css/style.css') -
FastAPI (Starlette) 的标准写法:参数名必须是
path,而不是filename。
三,解决:
检查你的 HTML 模板(Jinja2 模板),把 filename 改为 path:
浙公网安备 33010602011771号