# jinja2.exceptions.UndefinedError：'user' 未定义

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

## 根因

Jinja2 模板引用了未传递给渲染上下文或为 None 的变量。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   return render_template('index.html', user=current_user)
   ```
2. **** (85% 成功率)
   ```
   {% if user is defined %}{{ user.name }}{% endif %}
   ```

## 无效尝试

- **** — The default filter only works if the variable is undefined; if user is None, it still prints 'None'. (60% 失败率)
- **** — This silences the error but may lead to empty output and hides real bugs. (50% 失败率)
