Summary
In the Django Template Language (DTL), missing variables currently fail silently by returning an empty string. While string_if_invalid exists, it is a global setting that lacks the surgical precision modern developers need.
This project aims to introduce Ergonomic Control Over Missing Variables, providing a way to opt-in to stricter error reporting at a more granular level (per template or per block) without breaking backward compatibility.
Why this is important
-
Faster Debugging: Developers spend significant time hunting down typos in templates that don’t raise errors.
-
Production Safety: Prevent “broken” UI states where critical data is missing but the page still renders.
-
Modern DX: Aligns Django with modern frontend expectations of explicit error handling.
Key Goals
-
Granular Strictness: Allow developers to trigger exceptions for missing variables in specific scopes.
-
Enhanced Context: Improve error messages to include the template name and line number for missing variables.
-
Opt-in Philosophy: Ensure that existing projects remain unaffected unless they choose to use the new features.
Full Proposal & Technical Draft
I have outlined the implementation strategy, including potential changes to the Engine and Template classes, in my detailed proposal:
Read the Full Proposal on GitHub