You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask应用加载CSS资源报404错误求助:文件存在但无法加载

Flask静态资源404问题:CSS文件存在却无法加载

在Flask应用中加载CSS时,浏览器提示Failed to load resource: the server responded with a status of 404 (NOT FOUND)错误,但对应的CSS文件确实存在,且我认为文件结构是正确的。尝试了多种方法后,仍无法将CSS加载到base.html基础模板中。

文件结构

C:.
│   run.py
│   
├───instance
│       site.db
│       
├───jecco
│       models.py
│       routes.py
│       __init__.py
│
├───static
│       style.css
│
└───templates
        base.html

base.html代码

<!doctype html>
<html lang="es">
  <head>
      <!-- Required meta tags -->
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
      <title>MyProject</title>

  </head>
  <body>


      <header class="header">

        <div>
            <h1>Welcome</h1>
            <a href="{{ url_for('main.home')}}">Inicio</a>
            <a href="">Catalogo</a>
            <a href="">Rendimiento</a>
            <hr>
        </div>
 
      </header>

      <h1>Hello, world!</h1>

  </body>
</html>

CSS文件代码

.header{
     background-color: blueviolet;
     display: grid;
     grid-template-columns: 1fr 2fr;
 }

已尝试的解决方法

  • 将CSS文件移至templates同目录
  • 改用styles文件夹替代static目录
  • 使用相对路径和绝对路径引用CSS
    以上方法均未解决问题。

内容的提问来源于stack exchange,提问作者Ronald Poveda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 02:30:01