4

given a structure like inside a directory foo:

/web
    /static
        /css
        /img
/model
runapp.py

How to server static files from web/static/css or /img

like

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

It gives

werkzeug.routing.BuildError
BuildError: ('web/static', {'filename': 'css/bootstrap.min.css'}, None)

I have done

app = Flask(__name__, static_folder=os.path.join(os.getcwd(),'web','static'), static_url_path='')

but it doesn't work. and btw whats the difference between static_folder and static_url_path ?

user2290820
  • 2,709
  • 5
  • 34
  • 62

1 Answers1

4

url_for('web/static') won't work because 'static' is a special blueprint for serving static files. So do this:

url_for('static', filename='css/bootstrap.min.css')

And set the static folder in your app:

app = Flask(__name__, static_folder=os.path.join(os.getcwd(),'web','static'))

static_url_path defaults to the name of static_folder. Not sure how setting it to '' would help.

nathancahill
  • 10,452
  • 9
  • 51
  • 91