Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

flask

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?

like image 558
Avi Das Avatar asked Oct 13 '25 11:10

Avi Das


1 Answers

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.)

like image 135
Sean Vieira Avatar answered Oct 15 '25 06:10

Sean Vieira