本文详解 flask 应用中出现 “failed to load resource: 404 (not found)” 的根本原因,重点指出 `@app.route` 装饰器遗漏导致路由未注册,并系统性修复静态文件(css/js/图片)路径、目录结构及前端引用方式,确保 flask 正确提供静态资源。
在 Flask 开发中,浏览器控制台频繁报出 Failed to load resource: the server responded with a status of 404 (NOT FOUND),通常并非网络或服务器宕机问题,而是 Flask 未能正确识别并响应请求路径——尤其是对 /static/ 下的 CSS、JS 和图片资源的请求。结合你提供的项目结构和代码,问题根源有二:核心路由未注册 + 静态资源路径引用错误。
? 关键错误:路由装饰器缺失(导致首页 404,连带阻断静态资源加载)
你的 app.py 中存在严重语法错误:
app.route('/') # ❌ 缺少 '@',这不是装饰器!此行被完全忽略
def index():
return render_template('Home.html')
✅ 正确写法必须是带 @ 的装饰器:
@app.route('/')
def index():
return render_template('Home.html')
⚠️ 后果: