19

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__
1

1 Answer 1

25

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Ah, this is it! One quick question though, would the "url_for("static"" relate to the "static_folder" path set in my app?
When a static asset is requested, Flask's static endpoint will try to serve it from the folder specified for static_folder.
can i change the static folder i am working with npm to install npm package to the frontend so i need to change to node_modules
@juancarlospeñacabrera yes, that is what static_folder is for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.