'Flask app can't load CSS file from static folder

So my file directory looks like this:

/templates
--base.html
/static
--/css
----base.css

In the of my base.html file I have this line:

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

However, when I run the app the base.css file isn't applied to the base.html page. In my browser I get the following error in the console:

GET http://0.0.0.0:8081/static/css/base.css net::ERR_ABORTED 404 (NOT FOUND)

I've already tried clearing my browser cache, hard refreshing the page, and restarting the app, but it still doesn't work.



Solution 1:[1]

I've figured out the solution. I just needed to add the following to my __init__.py file:

package_dir = os.path.dirname(
    os.path.abspath(__file__)
)
static = os.path.join(
    package_dir, "static"
)
app.static_folder=static

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1