这个错误通常表示您在Flask应用程序上下文之外尝试访问Flask扩展或对象。 您需要在应用程序上下文中访问它们。 一种解决方法是在应用程序上下文中使用with语句包装代码块。
例如,以下代码块中的Post对象是Flask-SQLAlchemy实例db的实例,如果在应用程序上下文之外调用它,将引发RuntimeException。
from szh_web_server.models import Post
posts = Post.query.all()
for post in posts:
print(post)
要解决这个错误,可以通过with语句将代码块包装在应用程序上下文中。例如:
from szh_web_server import app
from szh_web_server.models import Post
with app.app_context():
posts = Post.query.all()
for post in posts:
print(post)
这将确保您在Flask应用程序上下文中使用db对象,并避免引发RuntimeError。
- RuntimeError application Working outside contextruntimeerror application working outside runtimeerror working outside context runtimeerror application sqlalchemy working runtimeerror application解决方案working dependencies application the context outside dita directory the outside outside source idea java syntaxerror statement错误outside working