在 heroku 上导出 FLASK_CONFIG='development'

export FLASK_CONFIG='development' on heroku

在我的 flask 应用程序中我 运行 命令 export FLASK_CONFIG='development' 在本地到 运行 应用程序。但是现在在 heroku 中,当我 运行 命令 heroku run init 得到一个错误,我知道我应该 import export FLASK_CONFIG='development 但是我怎么能在 heroku

错误:

(venv)peg@peg:~/Fast-monks$ heroku run python manage.py shell
Running `python manage.py shell` attached to terminal... up, run.6078
postgres://fboastbcwkwdsa:bIrdHsid_Zv3GOzyrhCB9x1oig@ec2-174-129-1-179.compute-1.amazonaws.com:5432/dfg3hpuotv5lal
postgres://fboastbcwkwdsa:bIrdHsid_Zv3GOzyrhCB9x1oig@ec2-174-129-1-179.compute-1.amazonaws.com:5432/dfg3hpuotv5lal
Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    app = create_app(os.environ['FLASK_CONFIG'] or 'default')
  File "/app/.heroku/python/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'FLASK_CONFIG'
WARNING: Toolbelt v3.28.4 update available.

manage.py:

app = create_app(os.environ['FLASK_CONFIG'] or 'default')
magrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)

config.py:

class Config(object):
    DEBUG = False
    SECRET_KEY = 'Thisismysecretkey'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL',
             'postgresql+psycopg2://peg:1234@localhost/app')
    print SQLALCHEMY_DATABASE_URI


class HerokuConfig(ProductionConfig):
    def init_app(cls, app):
        ProductionConfig.init_app(app)
        import logging
        from logging import StreamHandler
        file_handler = StreamHandler()
        file_handler.setLevel(logging.WARNING)
        app.logger.addHandler(file_handler)


config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'heroku': HerokuConfig,
    'default': DevelopmentConfig}

您应该通过heroku命令设置环境变量:

heroku config:set FLASK_CONFIG=development

虽然您的 manage.py 看起来不对,但您似乎在尝试设置默认值以防 var 不存在,但在它达到默认值之前出现了 keyerror。改用这个:

app = create_app(os.environ.get('FLASK_CONFIG', 'default'))