According to Django docs, the localize template tag allows for more fine-grained control of localization in templates than the general USE_L10N = True setting.
However, turning localize on doesn't produce the same result as setting USE_L10N = True when combined with the date filter.
# USE_L10N = True in settings.py
{% load l10n %}
{{some_datetime_value|date}}
# Date is shown and localized
vs
# USE_L10N = False in settings.py
{% load l10n %}
{% localize on %}
{{some_datetime_value|date}}
{% endlocalize %}
# Date is shown and NOT localized
Why are the two results different? How can I make the localize tag localize correctly in combination with the date filter?
As Brian Destura pointed out in the comments this looks like a Django bug.
Converting the datetime object to a date object before passing it to the template might work for a single object, but is very inconvenient if you have to access model fields that are stored as datetime (e.g. within for loops).
In case someone faces the same issue, a custom filter that converts the datetime object to a date object worked for me (this is actually what the in-built date filter is supposed to do).
custom_tags.py
@register.filter
def get_date(value):
return value.date()
template.html
{% load i10n %}
{% load custom_tags %}
{% localize on %}
{{some_datetime_value|get_date}}
{% endlocalize %}
#localizes the format correctly