无法加载 Flask 模板 css

Flask Templates couldn't load css

我按照本教程使用 Flask 开发模板 http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ii-templates

我的文件树是下一个:

/static
   /js
        bootstrap.min.js
        jquery_1.11.3.js
   /css
        bootstrap.min.css
   /images
/templates
        index.html
/python_venv
server.py

server.py代码:

     @app.route('/subdomain/')
     def getPrevisionPoblacion():
          return render_template('index.html')

和css link里面的index.html代码如下:

      <script src="/static/js/bootstrap.min.js"></script>
      <link rel="stylesheet" href="/static/css/bootstrap.min.css"/>
      <script src="/static/js/jquery_1.11.3.js"></script>

nginx 配置:

      location /subdomain/{
      root        /apps/subdomain/static;
      uwsgi_pass  unix:///tmp/subdomain.sock;
      include     uwsgi_params;
      }

当我在 Chrome 上检查它时,索引没有加载 CSS 文件。我用 Developer's tools 检查了网络,错误是 404。 我也尝试了我在这个未解决的问题上看到的类似代码但没有成功 Inline CSS background:url() not working in Jinja2 template

有什么帮助吗?

如果将静态文件直接放在 /static 文件夹下,这应该可以工作

<link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap.min.css') }}">

如果您不想这样,请按照此问题批准答案中的说明进行操作,

Link to Flask static files with url_for

问题出在服务器上。

我必须为 Flask 模板提供静态文件夹才能加载 css。

我在nginx配置文件中写了下:

    location /subdomain/static)/  {
       root    /opt/apps/content_folder/static;
    }

我不使用 url_for() 函数 y 将资源的 URL 写为:

    static/css/my_css_template.css

最后检查文件中的@app.route() 渲染模板是否正确,然后 flask 模板可以访问 css、图像和 js。