18

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__
charlesreid1
  • 4,360
  • 4
  • 30
  • 52
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
  • Does this answer your question? [How to serve static files in Flask](https://stackoverflow.com/questions/20646822/how-to-serve-static-files-in-flask) – ggorlen May 01 '23 at 18:32

1 Answers1

23

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>
dirn
  • 19,454
  • 5
  • 69
  • 74