# jinja2.exceptions.UndefinedError: 'dict 对象' 没有属性 'username'。

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

## 根因

模板引用了未传递给 render_template 的变量，或提供的对象上不存在的属性。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Use `{{ user.username if user else '' }}` or pass a default: `render_template('x.html', user=user or {})`.
   ```
2. **** (85% 成功率)
   ```
   Enable ChainableUndefined: `app.jinja_env.undefined = jinja2.ChainableUndefined` to allow nested access without raising.
   ```

## 无效尝试

- **** — Undefined has no __bool__ short-circuit before attribute access; still raises. (85% 失败率)
- **** — Default already is Undefined; needs ChainableUndefined or StrictUndefined change, not reassignment. (70% 失败率)
