简体   繁体   中英

Python Flask serving static files from root static folder rather than blueprint's static folder

I have a blueprint defined as :

auth_login = Blueprint('auth_login', __name__, template_folder='templates', static_folder='static', static_url_path='/static')

I am registering it as :

app.register_blueprint(auth_login, url_prefix='/user')

I have a route:

@auth_login.route('/<username>/')
def profile(username):
return render_template('profile/user.html',
username = username)

With this setup, the /user/ endpoint never manages to load the static files as I get,

"GET /user//static/css/lib/bootstrap.min.css HTTP/1.1" 404 - "GET /user//static/css/lib/font-awesome/css/font-awesome.min.css HTTP/1.1" 404 -

What I really want is to look into the static folder of the root application and not the blueprint, so I want it to request

"GET /static/css/lib/bootstrap.min.css HTTP/1.1" 200 -

Is it possible to configure this via static_folder or static_url_path so that the route will look for the static files at root and not relative to where the blueprint is mounted?

By default, Flask sets up the /static URL to serve files out of {application_root}/static - you are overwriting that default with your blueprint's code. (The URL is still /static but the folder is {application_root}/path/to/blueprint/static , which, by the sound of things, is not what you want).

Simply remove the static_folder and static_url arguments to your Blueprint initialization and the URLs should work just fine (assuming that you have the appropriate CSS in your {application_root}/static folder.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM