No Context data insertion abnormality No application found. Either work inside a view function or push an application context.

flask web development in Chapter VIII of the login test 8.4.7.

When we insert data, you have to import the appropriate package (manage for my startup file):

from app import create_app,db
from app.models import User,Role
from manage import app

Now enter User.query.all in python shell in the () will appear:

No application found. Either work inside a view function or push an application context. 

This error, according to the semantic context we know it should be an error, so we need to introduce a context:

And because each context app.app_context () Returns the object are different, and therefore with a temporary variable to store, when using push, run pop.

Add the following statement in python shell:

app_context=app.app_context()

app_context.push()

User.query.all()

app_context.pop()

We can solve No application found. Either work inside a view function or push an application context. This problem.

 

Also, if inserted does not go in, there may be because there are many relationship User and Role, so we will be referential integrity constraints conditions can be deleted.

Guess you like

Origin blog.csdn.net/hrbust_cxl/article/details/87482734