Sometimes, we want to fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy.
In this article, we’ll look at how to fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy.
How to fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy?
To fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy, we cakll app.app_context
to return the app context.
For instance, we write
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'My connection string'
db.init_app(app)
with app.app_context():
db.create_all()
to run our SQLAlchemy code in the app context by putting them inside the with
block.
We get the app context with app.app_context()
.
Conclusion
To fix ‘No application found. Either work inside a view function or push an application context.’ with Python Flask and Flask-SQLAlchemy, we cakll app.app_context
to return the app context.