#37015 new New feature

Improve template debugging by showing missing context variables and suggestions

Reported by: Butool Fatima Owned by:
Component: Template system Version: 6.0
Severity: Normal Keywords: template debugging, error reporting, undefined variables, developer experience, suggestion, debug mode
Cc: Butool Fatima Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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., emialemail)
  • 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.

Attachments (2)

Screenshot 2026-03-31 100327.png (7.5 KB ) - added by Butool Fatima 89 minutes ago.
Screenshot 2026-03-31 100511.png (104.2 KB ) - added by Butool Fatima 89 minutes ago.

Download all attachments as: .zip

Change History (2)

by Butool Fatima, 89 minutes ago

by Butool Fatima, 89 minutes ago

Note: See TracTickets for help on using tickets.
Back to Top