While listening to the latest Django Chat podcast with @Lily-Foote there was talk about ways to raise an error when a variable is missing from the template context.
A few ideas were mentioned that I remember:
global setting that changes the behavior for non-Django templates, i.e. Django admin templates use this functionality and so we can’t break that functionality; django-fastdev provides similar functionality now, I think?
a template tag that could be used to define a list of variables that should raise an exception if used and they are not in the context (I think that was the idea?)
similar to #2, but perhaps a way to mark per-template that all variables are required to be in the context?
One thing I was brainstorming was a way to specify, when using the variable, that it should always be non-null. Maybe a suffix added to the variable to indicate that it should be non-null?
In GraphQL, a schema uses a ! to indicate that the value should be non-null, so that was the first thing that popped into my head, but I’m curious if anyone has any other ideas about this?
I also wonder about adding ? to mark explicitly using the existing behaviour. This would allow Django’s admin, for example, to migrate to use ? where that behaviour is relied on. This would be more self-documenting, which I think should also make overriding/copying admin templates easier.
Good point! I do like the explicitedness of the ?.
Requiring a ? for the current behavior seems like it would require a long deprecation period, since I assume there is a lot of template code floating around that expects the current behavior. I guess there could be a setting to determine how it works, but I worry that it could be a messy transition.
Yeah. I’d suggest not getting too far ahead. The ! (and ? maybe?) idea is cute, a possibility. The idea of swapping out the default behaviour under the ecosystem (with any deprecation period) is a whole something else. (The ?-like behaviour is a defining feature of the DTL. A tag to enforce ! as default for that template, might be a better follow-up)
Turns out there’s an only ticket for this, of course. (The raising bit, not the optional/force unwrapping syntax)
There’s a PR for it, but I’m not sure it’ll work, as it’s only currently a simple toggle at the template engine level.
But take a look at Tim’s old comment:6 there. There some ideas and breadcrumbs for how we might make this more subtle and useful (allowing logging for example, rather than just raising).
I think if we could combine that with (maybe) the per-file thoughts from this thread we might be able to make progress on an old and recurring point.