# Jinja2未定义错误：'user'未定义

- **ID:** `python/flask-jinja2-undefinederror-variable-undefined`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

模板引用了一个未传递给模板上下文的变量。

## 版本兼容性

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

## 解决方案

1. **Pass variable in render_template** (95% 成功率)
   ```
   return render_template('index.html', user=current_user)
   ```
2. **Use default filter in template** (90% 成功率)
   ```
   {{ user | default('Guest') }}
   ```

## 无效尝试

- **Adding variable in wrong route** — Variable must be passed in the specific route that renders the template. (50% 失败率)
- **Using global variable without context** — Flask templates don't inherit global variables without explicit passing. (70% 失败率)
