Stupid.Fat.Cat
Stupid.Fat.Cat

Reputation: 11295

Setting up static folder path in Flask

Seems like my static files aren't being served properly. This is what it looks like right now:

myApp
    __init__.py
    static
       img
       js
         bootstrap.min.js
       etc.

This is what my app config in my __init__.py looks like:

app = Flask(__name__, static_url_path="", static_folder="static")

This is the error:

127.0.0.1 - - [01/Dec/2014 13:12:01] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 -

As far as the url routing goes, no problems there, localhost/home routes to home, localhost/contact routes to contact, etc. But static files aren't being found :( Am I missing something?

NOTE: I'm hosting this on my Mac, purely local host

This is my __init__.py main method:

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)

Running as:

python __init.py__

Upvotes: 19

Views: 46137

Answers (1)

dirn
dirn

Reputation: 20739

It looks like you are hardcoding to static file URLs. You tell Flask to server them with no URL prefix -- the default would have been /static -- but the log shows the request going to /static/js/bootstrap.min.js. You probably have something like the following in a template file.

<script src="/static/js/bootstrap.min.js"></script>

Instead, you should use url_for to create the URL for you. Change the above to

<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>

This will result in

<script src="/js/bootstrap.min.js"></script>

Upvotes: 25

Related Questions