将Flask应用程序部署到heroku会导致create_app()出现500错误

时间:2019-12-13 01:59:40

标签: python heroku flask

我正在尝试将一个应用程序部署到heroku,该应用程序可以使用“ Flask run”在我的本地计算机上正常运行。我的应用程序的结构类似于带有蓝图的微瓶烧瓶教程。当我部署它然后尝试访问网站时,出现此错误:

TypeError: create_app() takes from 0 to 1 positional arguments but 2 were given

这是我的__init__.py文件:

db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()
login.login_view = 'auth.login'
login.login_message = 'Please log in to access this page.'
bootstrap = Bootstrap()    
uploadFolder = os.path.dirname(__file__) + '/uploads/companies'


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

db.init_app(app)
migrate.init_app(app, db)
login.init_app(app)
bootstrap.init_app(app)

from app.errors import bp as errors_bp
app.register_blueprint(errors_bp)
from app.auth import bp as auth_bp
app.register_blueprint(auth_bp, url_prefix="/auth")
from app.main import bp as main_bp
app.register_blueprint(main_bp, url_prefix="/")


if not app.debug and not app.testing:
    if not os.path.exists('logs'):
        os.mkdir('logs')
    file_handler = RotatingFileHandler('logs/StructuredSafety.log', maxBytes=10240,
                                        backupCount=10)
    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)

    app.logger.setLevel(logging.INFO)
    app.logger.info('Structured Safety startup')

return app

from app import models

我的app.py文件是:

from app import create_app as application
from app import db
from app.models import User

app = application

我的Procfile看起来像这样:

web: flask db upgrade; gunicorn StructuredSafety:app

我一直在努力寻找有关此问题的在线资源。我知道我的app.py文件正在调用create_app,但是我没有在其中传递任何内容,所以我很好奇create_app的实际调用位置,以及为什么它要接受许多参数。

1 个答案:

答案 0 :(得分:2)

您需要调用create_app来获取Gunicorn将提供的应用程序。

app.py中执行:

from app import create_app
app = create_app()

然后将您的Procfile更改为此:

web: flask db upgrade; gunicorn app:app