Flask-Assets在本地工作但不在Heroku上工作

时间:2016-03-18 14:10:24

标签: python heroku flask flask-assets webassets

我正在努力修改cookiecutter Flask应用。我在WIN7上本地工作并尝试部署到heroku。我目前正在尝试向网页添加日期选择器。我找到了https://eonasdan.github.io/bootstrap-datetimepicker/。 myflaskapp的结构:

enter image description here

通过在文档根目录中使用.bowerrc文件,我设置了bower来在静态根目录下安装前端依赖项:

{ "directory" : "myflaskapp/static/bower_components" }

enter image description here

此cookiecutter使用flask-assets来管理项目资产。在https://adambard.com/blog/fresh-flask-setup/之后我修改了myflaskapp / assets.py文件:

from flask_assets import Bundle, Environment
import os

css = Bundle(
    "libs/bootstrap/dist/css/spacelab/bootstrap.css",
    "bower_components/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.css",
    "css/style.css",
    "css/home.css",
    # "css/style.css",
    filters="cssmin",
    output="public/css/common.css"
)

js = Bundle(
    "libs/jQuery/dist/jquery.js",
    "libs/bootstrap/dist/js/bootstrap.js",
    "bower_components/moment/moment.js",
    "bower_components/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js",
    "js/plugins.js",
    filters='jsmin',
    output="public/js/common.js"
)


assets = Environment()

assets.register("js_all", js)
assets.register("css_all", css)

myflaskapp / app.py初始化资产:

from flask import Flask, render_template
from myflaskapp.assets import assets
    """
    :param config_object: The configuration object to use.
    """
    app = Flask(__name__)
    app.config.from_object(config_object)
    register_extensions(app)
    register_blueprints(app)
    register_errorhandlers(app)
    return app

def register_extensions(app):
    assets.init_app(app)

def register_blueprints(app):
    app.register_blueprint(public.blueprint)
    app.register_blueprint(user.blueprint)

在myflaskapp / settings.py中,在测试期间,我设置了:

ASSETS_DEBUG = True  # Don't bundle/minify static assets

在本地,控制台中没有错误。在heroku上,我收到内部服务器错误。日志:

    ←[33m2016-03-19T18:02:12.464159+00:00 app[web.1]:←[0m     for org, cnt in self.resolve_contents(ctx):
    ←[33m2016-03-19T18:02:12.464161+00:00 app[web.1]:←[0m     raise BundleError(e)
    ←[33m2016-03-19T18:02:12.464162+00:00 app[web.1]:←[0m BundleError: 'bower_components/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.css' not found in load path:
    ['/app/myflaskapp/static']
    ←[33m2016-03-19T18:02:12.464150+00:00 app[web.1]:←[0m     rv = template.render(context)
    ←[33m2016-03-19T18:02:12.464151+00:00 app[web.1]:←[0m   File "/app/.heroku/python/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    ←[33m2016-03-19T18:02:12.464152+00:00 app[web.1]:←[0m   File "/app/.heroku/python/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    ←[33m2016-03-19T18:02:12.464154+00:00 app[web.1]:←[0m     {% extends "layout.html" %}
    ←[33m2016-03-19T18:02:12.464157+00:00 app[web.1]:←[0m     urls = bundle.urls()
    ←[33m2016-03-19T18:02:12.464153+00:00 app[web.1]:←[0m     reraise(exc_type, exc_value, tb)

正如您在顶部屏幕截图中看到的那样。我该如何解决这个问题?

注意:我一直在关注https://github.com/mitsuhiko/flask/issues/348,这可能与此有关。

1 个答案:

答案 0 :(得分:3)

尝试检查.gitignore,通常会忽略构建,这会排除静态文件:bower_components / eonasdan-bootstrap-datetimepicker / build / *