Running HTML file won't recognize static files linked through url_for

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

Add Comment
1 Answer(s)

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.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.