flask web开发第八章中8.4.7的登入测试。
在我们进行数据插入时,先要导入相应的包(manage为我的启动文件):
from app import create_app,db
from app.models import User,Role
from manage import app
此时在python shell中输入User.query.all()会出现:
No application found. Either work inside a view function or push an application context.
这个错误,根据语意我们得知应该是上下文出现了错误,因此我们需要引入一个上下文:
又因为每次app.app_context()返回的上下文是不同的对象,因此用一个临时变量来存储,用的时候push,用完pop。
在python shell中加入如下语句:
app_context=app.app_context()
app_context.push()
User.query.all()
app_context.pop()
即可解决No application found. Either work inside a view function or push an application context. 这个问题。
此外如果插入不进去,还有可能是因为User和Role中存在一对多的关系,因此我们将其参照完整性约束的条件删除即可。