RuntimeError: Working outside of application context.
时间: 2023-09-26 17:08:04 浏览: 133
This error occurs when you are trying to access or modify data that is tied to the Flask application context, but you are not currently within that context.
For example, if you try to access a Flask extension or request data outside of a Flask route function, you may encounter this error.
To resolve this error, you need to make sure that you are operating within the Flask application context. This can be achieved by wrapping your code within a Flask route function or by using the `with app.app_context()` block.
Here's an example of using `with app.app_context()`:
```python
from flask import Flask
app = Flask(__name__)
with app.app_context():
# your code that requires the Flask application context here
```
Alternatively, you can also use the `current_app` proxy object from the `flask` module to access the Flask application context from within your code:
```python
from flask import current_app
# your code that requires the Flask application context here
current_app.some_extension.do_something()
```
Remember to always make sure you are within the Flask application context before accessing or modifying any data related to your Flask application.
阅读全文