-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Static files only work for blueprints registered with url_prefix #348
Comments
It doesn't work correctly with |
I ran into this issue as well and found that using |
I was able to workaround this problem by setting the app's static_folder, e.g: from flask import Flask
from .api import blueprint as api_blueprint
from .ui import blueprint as ui_blueprint
app = Flask(__name__, static_folder = './ui/static')
app.register_blueprint(ui_blueprint)
app.register_blueprint(api_blueprint, url_prefix = '/api') Basically the root app is still handling the static files, it just knows to find them in the blueprint's static folder instead of the default. |
I had the same unpleasant surprise when testing out my blueprint inside an app. I can't use the blueprint static folder as the app's static folder like mattsmalley suggests (would like my blueprint to remain sandboxed and untouched). I don't want to mount my blueprint on a I don't know if there is a reason for this, but it would be nice for a blueprint registered with no |
This seems like a bug, rather than needing additional documentation @deshipu . Currently, if one of more blueprints does not use a url_prefix, all of their static files must be mashed together in the application static folder which is counterproductive for blueprint isolation. Adding a dummy url_prefix to get around this issue is not desirable either. Also @mattsmalley's solution only works if there is only one blueprint operating without a url_prefix. @artnez What did you set the static_url_path to? I've tried '/myblueprint/static' and a variety of similar constructions, but url_for('myblueprint.static', filename='test.js') causes a BuildError. Couldn't find any docs on static_url_path and that static_url_path does not make http://localhost:5000/myblueprint/static/test.js available as expected. |
Another option. Add a symbolic link to your app's static folder that links in the blueprint's static folder. So for example:
Then refer to static content in the blueprint as /static/users/(js, img, etc.) |
After I posted the symbolic link suggestion, I looked in the source code and figured out what @artnez meant by using "static_url_path". @dghhubble I tried the same thing you did and it works for me. It appears that static_url_path has the same meaning as it does on the Flask object. |
A solution involving blueprint = Blueprint(
'BLUEPRINTNAME',
__name__,
static_folder='static',
static_url_path='%s/BLUEPRINTNAME' % app.static_url_path
) |
Duplicate of #226? I'd leave this open though, as it contains more information. |
This can be solved extending the application. For example: from flask import Flask
from flask.helpers import send_from_directory
from os import path
class MyCustomApp(Flask):
def send_static_file(self, filename):
for blueprint_name, blueprint in self.blueprints.items():
filepath = path.join(blueprint.static_folder, filename)
if path.exists(filepath):
return send_from_directory(blueprint.static_folder, filename)
return super(MyAdminApp, self).send_static_file(filename)
app = MyCustomApp(__name__)
app.register_bluprint(...) |
Finally, I solved this problem by using the para 'static_url_path'... But I still wonder if it's a bug of flask-blueprint or something? |
I'm also resolving this issue with
|
I have the same issue -- mounting a Blueprint with a |
Move templates and static assets to corresponding blueprint package, fixup a few references (url_for calls). Leave openid at root (instead of moving to sphinxweb.auth) because pallets/flask#348 (can't mount blueprint static files to /static) & it is referred to from the root CSS. Maybe look into e.g. flask-assets to see if it can handle these cases correctly and/or CSS concatenation so auth CSS can be in auth blueprint? Though can also just load multiple CSS (can blueprint override base to add CSS? Maybe use blueprint-local base?)
https://vilimpoc.org/blog/2012/11/21/serving-static-files-from-root-and-not-static-using-flask/ |
Something very strange is up when using
then
then UpdateOkay, after beating this up for several more hours, and no thanks to the docs or examples scattered about the net, I think I partly understand this now -- and I don't have to move my files 🎆. I had to turn my index.html into a template to see what path Flask would generate with
I chose to hard code it (it's a React app and templating would be overkill... and anti-React). I also found what does and does not work with
So basically, Lesson learned: Stay away from That might apply to this issue #521 as well. Anyway, don't know how this may or may not apply to Blueprints but, there it is. Perhaps this will be useful to someone headed down the same path or for writing better examples and docs. |
Hey guys, thank you @juanitogan, we have been working on this for days and your post really solved it, thanks! |
I manually created the rules instead of registering the blueprint
|
It works for me when:
And here's my app structure:
|
@DusanMadar that isn't what this issue is about: you're not mounting the blueprint at |
workaround: |
I also happened to run into this issue and I am now at the point that the static folder merges with the application static folder as needed but since I mounted it at a prefix there are 2 related problems. Only the registration time prefix works as described above. The one in the Blueprint init is not the same. The other problem is that the url_for template tag is broken for these static files as they are mounted at /static/whatever but the blueprint prefix is computed and prepended in front of it And this makes it a functionality issue not a documentation one. |
Please refer to the discussion in: pallets/flask#348 I used the method suggested by mattsmalley. The key modification is adding static_folder = './main/static' in the sentence: app = Flask(__name__, static_folder = './main/static') in app\__init__.py Then change the url "/main/*" to url "/*" in code (for example, loadmap.js, mainmenu).
Found easy solution. Do it only for your page with
from flask import render_template, Blueprint
main_page_blueprint = Blueprint(
'main_page',
__name__,
template_folder='templates',
static_folder='static',
static_url_path='/project.main_page.static'
)
@main_page_blueprint.route('/')
def index():
return render_template('main_page/index.html')
{% block content%}
<img src="{{url_for('main_page.static', filename='img/photo.png')}}" alt="">
{% endblock %}` in from project.main_page.view import main_page_blueprint
app.register_blueprint(main_page_blueprint, url_prefix='/') Hope it works! |
Move templates and static assets to corresponding blueprint package, fixup a few references (url_for calls). Leave openid at root (instead of moving to sphinxweb.auth) because pallets/flask#348 (can't mount blueprint static files to /static) & it is referred to from the root CSS. Maybe look into e.g. flask-assets to see if it can handle these cases correctly and/or CSS concatenation so auth CSS can be in auth blueprint? Though can also just load multiple CSS (can blueprint override base to add CSS? Maybe use blueprint-local base?)
Hi, mine worked with this set up: Folders structures:
In my_blueprint file:
In my templates files:
Hope this will help! |
The option
static_folder
for blueprints only works correctly when the blueprint has been registered with aurl_prefix
, otherwise the application's default static URL takes precedence.There is nothing wrong in that behavior, but it is not documented. It would be nice to have that fact mentioned at http://flask.pocoo.org/docs/blueprints/#static-files and/or http://flask.pocoo.org/docs/api/#blueprint-objects
The text was updated successfully, but these errors were encountered: