user2290820
user2290820

Reputation: 2759

Flask: Serve static files like css from different folder

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 ?

Upvotes: 4

Views: 2766

Answers (1)

nathancahill
nathancahill

Reputation: 10850

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.

Upvotes: 4

Related Questions