No application found. Either work inside a view function or push
程序员文章站
2022-04-01 15:50:46
...
No application found. Either work inside a view function or push
在使用flask的时候出现这种报错,应该是在部署蓝图的时候想要创建数据表出现了缺少命令配置的操作,可以试一试这一种方法
pip install flask_migrate
pip install flask_script
1.首先安装这两个模块,这是两个配合蓝图的模块,pip install flask_migrate惰性建表,pip install flask_script配置命令行,不会像create_all()和drop_all()这样每一次更新表都要删了重建
2.在flask项目根目录下创建manage.py,与app.py等同级,不要弄错了
3.在manage.py中添加
from flask_migrate import Migrate,MigrateCommand
from flask_script import Shell,Manager
from app import app
from models import db
#让其受指令控制
manage = Manager(app)
#绑定数据可迁移指令
migrate = Migrate(app,db)
#添加指令到数据库指令包当中
manage.add_command('db',MigrateCommand)
if __name__ == '__main__':
#runsever时,开启服务
manage.run()
app是app.py中的app = Flask(name)
db为我在models中实例化的SQLAlchemy ,也就是db=SQLAlchemy()
然后逐个配置Manager和Migrate
4.使用指令创建/更新数据库
python manage.py dn init
python manage.py dn migrate
python manage.py dn upgrade
5.大功告成,打完收工
推荐阅读
-
No application found. Either work inside a view function or push an application context. (Flask报错解决)
-
Flask 报错 RuntimeError: No application found. Either work inside a view function or push an applicati
-
No application found. Either work inside a view function or push
-
RuntimeError: No application found. Either work inside a view function or push an application contex