3

I'm having an issue that I really hope someone can keep me from banging my head against the table about.

  • I have a Flask app with multiple Blueprints that each have their own static and templates directories.
  • When only one Blueprint is registered and utilized the assets URL for bundled CSS and Javascript are both correctly resolved
  • However, when two or more Blueprints are resolved only the URLs for the assets in the last registered Blueprint resolve correctly (everything else seems to think that it's assets should be served out of the root static directory)

I'm getting incorrect asset URLs when templates from the Blueprints resolve the asset URLs. The URLs resolved are built incorrectly as if they should be served from a root static directory (which I would still like the overall app to have a static directory so that 400 and 500 errors can render pages using that):

http://{host}/static/public_css_app (404)

When it should be:

http://{host}/mod_core/static/public_css_app (200)

Here is part of the initiali(s)zation of the app

app = Flask(__name__)
# Import a module / component using its blueprint handler variable
# NOTE: The order that the Blueprints are imported in seems to relate to which one actual gets it's asset URLs rendered correctly. The last blueprint imported is the only one that works.
from appy.modules.core.controllers import mod_core
from appy.modules.auth.controllers import mod_auth

# Register blueprint(s)
appy.register_blueprint(mod_core)
appy.register_blueprint(mod_auth)

# Tried this method of setup and didn't do anything differently
# from flask.ext.assets import Bundle, Environment
# assets = Environment()
# assets.init_app(application)

Here's an example of one the Blueprint registrations inside of "{blueprint folder}/views.py":

One Blueprint's view:

mod_core = Blueprint('mod_core', __name__, url_prefix='/c',   template_folder='templates',  static_folder='static', static_url_path='/modules/core/static')
from . import bundles

Another Blueprint's view:

mod_auth = Blueprint('mod_auth', __name__, url_prefix='/auth',   template_folder='templates',  static_folder='static', static_url_path='/modules/auth/static')
from . import bundles

Here's an example of a bundles.py file (e.g. "/{blueprint_directory}/bundles.py"):

from appy import app
from flask import Flask
from flask.ext.assets import Bundle, Environment

bundles = {

   'public_js_app: Bundle(
        'mod_core/app/js/jquery.js',
        'mod_core/app/js/app.js',
        output='mod_core/b/app.js',
        filters='rjsmin'),

    'public_css_app': Bundle(
        'mod_core/app/css/style.css',
        'mod_core/app/font-awesome/css/font-awesome.css',
        output='mod_core/b/app.css',
        filters='cssmin'),
}

assets = Environment(app)
assets.register(bundles)

The templates are technically working and static files using url_for are working just fine, it's only the assets resolution that's not working.

{% assets "public_css_app" %}
<link rel="stylesheet" href="{{ ASSET_URL }}" type="text/css" />
{% endassets %}

Any help would be greatly appreciated as I'm new to Flask.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
jon333
  • 821
  • 2
  • 16
  • 28

0 Answers0