#37015: Improve template debugging by showing missing context variables and
suggestions
-------------------------------------+-------------------------------------
Reporter: coder_787 | Type: New
| feature
Status: new | Component: Template
| system
Version: 6.0 | Severity: Normal
Keywords: template debugging, | Triage Stage:
error reporting, undefined | Unreviewed
variables, developer experience, |
suggestion, debug mode |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
When working with Django templates, missing or undefined context variables
often fail silently or provide limited debugging information. This makes
it difficult to quickly identify the source of the issue.
For example:
```django
{{ user.email }}
```
If `user` or `email` is missing, Django does not clearly indicate which
variable is missing, where it originated, or whether a similar variable
exists in the context.
This proposal aims to improve template debugging by enhancing the error
reporting system in DEBUG mode.
The suggested improvements include:
* Detecting missing or undefined variables during template rendering
* Showing the missing variable name
* Indicating the template file and line number where the issue occurs
* Providing suggestions for similar variable names from the context (e.g.,
`emial` → `email`)
* Optionally introducing a setting like `STRICT_TEMPLATE_MODE` to raise
explicit errors for undefined variables
Example output:
```
Template Variable Error:
- Variable 'emial' not found in context
- Did you mean: 'email'?
- Template: profile.html, line 12
```
Benefits:
* Improves debugging experience
* Helps developers identify issues faster
* Reduces silent failures in templates
* Makes Django more beginner-friendly
Potential concerns:
* Performance impact (can be limited to DEBUG mode)
* Backward compatibility (can be optional via settings)
Implementation notes:
* Extend template variable resolution logic
* Integrate with existing error handling (e.g., VariableDoesNotExist)
* Use string similarity to suggest variable names
* Enable via settings or DEBUG mode only
A prototype of this feature has been implemented locally and is ready for
feedback and further refinement.
--
Ticket URL: <
https://code.djangoproject.com/ticket/37015>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.