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

posted @ 2026-07-17 19:05  刘宏缔的架构森林  阅读(3)  评论(0)    收藏  举报