Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

python - Flask blueprint static directory does not work?

According to the Flask readme, blueprint static files are accessible at blueprintname/static. But for some reason, it doesn't work.

My blueprint is like this:

  • app/frontend/views.py :

    frontend = Blueprint('frontend', __name__, 
                         template_folder='templates',
                         static_folder='static')
    
    @frontend.route('/') etc...
    
  • app/frontend/js/app.js : my javascript

  • Blueprint registered in Flask app (routes work and everything)

When I go to abc.com/frontend/static/js/app.js, it just gives a 404.

When I follow the Flask readme to get my static files:

<script src="{{url_for('frontend.static', filename='js/app.js')}}"></script>

The output is

<script src="/static/js/app.js"></script>

Which doesn't work either. There's nothing in my root app/static/ folder.

I can't access any static files in my blueprint! Flask read me says that it should work!

admin = Blueprint('admin', __name__, static_folder='static')

By default the rightmost part of the path is where it is exposed on the web. Because the folder is called static here it will be available at the location of the blueprint + /static. Say the blueprint is registered for /admin the static folder will be at /admin/static.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I include an argument to the static_url_path parameter to ensure that the Blueprint's static path doesn't conflict with the static path of the main app.

e.g:

admin = Blueprint('admin', __name__, static_folder='static', static_url_path='/static/admin')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...