Flask templates could not load css

I followed this guide to design flask templates http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ii-templates

My file tree is as follows:

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

      

server.py code:

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

      

And the css link inside the code.html code looks like this:

      <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 config:

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

      

When I check it on Chrome, the index doesn't load CSS files. I checked the web with developer tools and the error is 404. I tried also similar code I saw on this unresolved question with no success Inline CSS background: url () does not work in Jinja2 template

Any help on this?

+3


source to share


2 answers


The problem was with the server.

I had to serve a static flask folder that can load css.

I wrote the following in the nginx config file:

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

      



I am not using url_for () y function by specifying the resource url as:

    static/css/my_css_template.css

      

Finally, check @ app.route () inside a file that renders the template correctly, then the flash template can access the css, images and js.

+1


source


if you put static files directly in / static folder this should work

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

      



if you don't want that, follow the instructions in the answer to this question,

Link to static Flask files with url_for

0


source







All Articles