# 运行时错误：在请求上下文之外工作。

- **ID:** `python/flask-test-client-context-missing`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在请求上下文之外使用 flask.request 或 session，例如在测试设置或后台任务中。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   with app.test_request_context('/'):
    # use request
   ```
2. **** (80% 成功率)
   ```
   For background tasks, pass request data explicitly instead of using context.
   ```

## 无效尝试

- **** — Must use with app.test_request_context(): to push context. (85% 失败率)
- **** — Request context is tied to thread; async tasks may not have it. (90% 失败率)
