nginx - 使用 Gunicorn 运行应用程序时未注册 SQLAlchemy 扩展

标签 nginx flask flask-sqlalchemy gunicorn

我有一个正在开发中的应用程序,但是当我尝试使用 Gunicorn 运行它时,它给出了“sqlalchemy 扩展未注册”的错误。从我读过的内容来看,我似乎需要调用 app.app_context()某处,但我不确定在哪里。我该如何解决这个错误?

# run in development, works
python server.py

# try to run with gunicorn, fails
gunicorn --bind localhost:8000 server:app

AssertionError: The sqlalchemy extension was not registered to the current application.  Please make sure to call init_app() first.
server.py :
from flask.ext.security import Security
from database import db
from application import app
from models import Studio, user_datastore

security = Security(app, user_datastore)

if __name__ == '__main__':
    # with app.app_context(): ??
    db.init_app(app)
    app.run()
application.py :
from flask import Flask

app = Flask(__name__)
app.config.from_object('config.ProductionConfig')
database.py :
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()

最佳答案

仅当您使用 python sever.py 启动您的应用程序时是 if __name__ == '__main__':块命中,您在其中使用您的应用程序注册您的数据库。

你需要移动那条线,db.init_app(app) ,在那个街区之外。

关于nginx - 使用 Gunicorn 运行应用程序时未注册 SQLAlchemy 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30764073/

相关文章:

python - 如何正确运行查询 Flask-SQLAlchemy 数据库的连续测试?

nginx - 子子域被 NGINX 中的子域覆盖

php - nginx:Mac OS X 上带有 Curl/https 的 502 Bad Gateway

django - React请求Django API时,出现net::ERR_SSL_PROTOCOL_ERROR

laravel - phpmyadmin 显示 404 在 nginx 中找不到

python - 导入错误 : No module named app

database - 将字典插入sqlite3

python - 查询以检查集合大小是否为 0 或在 SQLAlchemy 中为空?

python - 访问我的本地 flask Web 应用程序时,蝗虫请求返回错误 502

python - Flask-admin 是否在运行时导入 html 模板?