【发布时间】:2019-06-27 10:58:25
【问题描述】:
我有一个包,其中包含我想在应用程序中重用的静态文件。基于https://webassets.readthedocs.io/en/latest/environment.html#webassets.env.Environment.load_path我想出了如下代码sn-p,用于每个应用程序的__init__.py(共享包为loutilities):
with app.app_context():
# js/css files
asset_env.append_path(app.static_folder)
# os.path.split to get package directory
asset_env.append_path(os.path.join(os.path.split(loutilities.__file__)[0], 'tables-assets', 'static'))
但是当ASSETS_DEBUG = False 时,这会导致在包中找到的文件之一出现 ValueError 异常。 (请参阅https://github.com/louking/rrwebapp/issues/366 了解详细的回溯——这可能与https://github.com/miracle2k/webassets/issues/387 有关)。
ValueError: Cannot determine url for /var/www/sandbox.scoretility.com/rrwebapp/lib/python2.7/site-packages/loutilities/tables-assets/static/branding.css
将代码更改为使用 url 参数,该参数现在适用于 ASSETS_DEBUG = False
asset_env.append_path(os.path.join(os.path.split(loutilities.__file__)[0], 'tables-assets', 'static'), '/loutilities')
但是现在当ASSETS_DEBUG = True 时,我看到文件无法在 javascript 控制台中加载
Failed to load resource: the server responded with a status of 404 (NOT FOUND) branding.css
已经使用如下不优雅的代码解决了 Catch-22,但想知道如何选择适用于 ASSETS_DEBUG = True 或 False 的 append_path() url 参数。
with app.app_context():
# js/css files
asset_env.append_path(app.static_folder)
# os.path.split to get package directory
loutilitiespath = os.path.split(loutilities.__file__)[0]
# kludge: seems like assets debug doesn't like url and no debug insists on it
if app.config['ASSETS_DEBUG']:
url = None
else:
url = '/loutilities'
asset_env.append_path(os.path.join(loutilitiespath, 'tables-assets', 'static'), url)
【问题讨论】: