0

For some odd reason, my CSS files are not linking when I run my HTML file. I'm creating a Flask app and am using url_for() to link my static files to my HTML file.

My CSS files are in a folder in the root directory called 'css' and the file is called 'style.css'

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

As an added measure, I also went ahead and added app.static_folder = 'static' to my main.py, cleared my cache, tried to reload using CMD + Shift + R and it still won't work.

I also have all my JS files and images linked using url_for() and nothing except the base HTML content shows up when I run the file in my browser. Any tips? Thanks

rgoel
  • 1
  • Does this answer your question? [Link to Flask static files with url\_for](https://stackoverflow.com/questions/16351826/link-to-flask-static-files-with-url-for) – jignatius Jul 13 '20 at 07:17

1 Answers1

0

Try to create the following structure for your project:

.
├── app.py
├── static
│   ├── css
│       ├── style.css
│       └── more.css
│   └── js
├── templates
│   ├── base.html
│   └── index.html

I think the problem is that flask is not able to find the files in your case as you stored the files in the root. Naming convention and directory structure has an importance here. Flask automatically looks for templates folder for templates and static folder for static files.

AMIT
  • 176
  • 2
  • 16
  • Thanks for your response. I actually have the exact same structure that you've described also with an images folder in my static directory as well! Super confused about why it won't work – rgoel Jul 16 '20 at 08:16