I am looking for a django setting or programmatic way to make all django template tags show the empty string when the value is None. For example, imagine that I have some django template:
{{cat}} chases {{mouse}}
if both cat and mouse are None, it will render as:
None chases None
I am aware that I can set each one using {{cat|default:""}} or {{mouse|default_if_none:""}}
However, I am looking for some sort of setting that would allow me to set the default for all tags, without explicitly adding |default:"" to every tag.
I am also aware of a setting called TEMPLATE_STRING_IF_INVALID. However, this setting applies only to invalid strings. None is considered valid.
No such thing exists. That’s why the
defaultanddefault_if_nonefilters exist. This is a feature; it makes you think about what you’re doing instead of relying on some behavior that would often times be misleading. If there’s a potential for a variable to beNone, then you should plan for that contingency. If the variable should always have some value, then the "None" indicates something is not right. If the default was to just render a blank string, then you would not know whether the value is not defined or is actually a blank string. Write coherent code and forget about shortcuts.