python
runtime_error
ai_generated
true
jinja2.exceptions.UndefinedError: 'dict object' 没有属性 'username'
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'username'
ID: python/starlette-template-undefined-error
80%修复率
87%置信度
0证据数
2024-06-18首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
模板引用了传递给 TemplateResponse 的上下文字典中不存在的变量属性。
English
The template references a variable attribute that is not present in the context dict passed to TemplateResponse.
解决方案
-
94% 成功率
{{ user.get('username', 'guest') }} -
92% 成功率
return templates.TemplateResponse('p.html', {'request': request, 'user': user or {}})
无效尝试
常见但无效的做法:
-
85% 失败
StrictUndefined makes the error louder, not absent; it does not supply the missing key.
-
80% 失败
Hides the bug and returns a broken page.