The Django Templating automatically localizes numbers. I’m looking for a way to disable this default behavior and make it opt-in, as in only localize values where I explicitly want it.
I find this default behavior very irritating because usually an unformatted value is not a (technical) problem, but a formatted one can cause issues due to no longer being a number[^] but a “string”. One instance where this is tricky is for example in a DataTables table where you want to show a formatted value in the table cell but want to use the actual number without formatting inside a data-*
attribute for sorting.
I stumbled upon the setting USE_L10N
that was removed in Django 5. I gathered that this setting would have disabled this default behavior and would be exactly what I wanted.
Now I’m left with the localize
template tags. I’m thinking of just wrapping my base template with {% localize off %}
, i.e.:
{% load l10n %}
<!doctype html>
<html>
<head>
...
</head>
<body>
...
{% localize off %}
{% block content %}{% endblock %}
{% endlocalize %}
...
</body>
</html>
This actually works as I would expect, and other templates included in the content
block have localization off. And I can apply the |localize
tag when needed.
But I’m unsure if this can cause other issues due to the “large scope” of such a localize off
block?
Another consideration would be to just always be explicit and use |[un]localize
on any value, and by doing so not “breaking” with an established default inside Django templates. Tough, it feels a bit cumbersome and comes back to this “magic” potentially breaking stuff if the unlocalize
is forgotten.
So any ideas or comments regarding this topic would be appreciated!