Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33281 closed Uncategorized (invalid)

intcomma template filter doesn't depend on the USE_L10N setting.

Reported by: Demetris Stavrou Owned by: nobody
Component: Template system Version: 3.2
Severity: Normal Keywords:
Cc: Claude Paroz Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given the following configuration:

# settings.py
LANGUAGE_CODE = 'el-gr'
USE_L10N = False
USE_THOUSAND_SEPARATOR = True
THOUSAND_SEPARATOR = ","
NUMBER_GROUPING = 3
# views.py
class ThousandView(TemplateView):
    template_name = "thousand_template.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["test_number"] = 1234
        return context
# thousand_template.html
{% load humanize %}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  {{ test_number|intcomma }}
</body>
</html>

one would expect that the test_number value is rendered as 1,234. However, it is rendered as 1.234. I intentionally used el-gr because the locale defines the thousand separator to be .. As shown in the settings, this shouldn't matter since we use USE_L10N=False. However, it seems that it affects the result, because if en-us is used instead, then the number is rendered as 1,234.

Testing this with Django version 3.1.13, works as expected, i.e. given the above configuration, the number is correctly rendered as 1,234.

Change History (5)

comment:1 by Mariusz Felisiak, 2 years ago

Cc: Claude Paroz added
Component: UncategorizedTemplate system
Resolution: invalid
Status: newclosed
Summary: Thousand separator is the wrong characterintcomma template filter doesn't depend on the USE_L10N setting.

It's an intentional change and is mentioned in the release notes:

"The intcomma and intword template filters no longer depend on the USE_L10N setting."

see also bd4e409695f8d6deebb1b3f70ffb9bc670477c2a.

comment:2 by Demetris Stavrou, 2 years ago

Thank you for the information. However, I was not able to use a comma as a thousands separator. I went through the documentation multiple times, and it is not apparent how to achieve this. If the intcomma is removed from the template, then the thousands separator disappears.

Maybe I am missing something, but based on the above, there seems to be an issue; it could be in the documentation not being clear?

comment:3 by Claude Paroz, 2 years ago

I guess that for your use case, the better would be to create your own intcomma filter that calls the Django intcomma with use_l10n param to False.

comment:4 by Demetris Stavrou, 2 years ago

Thank you for suggesting how to solve this problem on my project. However, the reason I opened this ticket was to help improve Django development experience. I do believe that the documentation could improve to better explain how this works. Honestly, it is not clear to me yet, what is going on. I am walking you through my thought process, when looking at the documentation:

Note, however, that to enable number formatting with thousand separators it is necessary to set USE_THOUSAND_SEPARATOR = True in your settings file. Alternatively, you could use intcomma to format numbers in your template.

Starting from here, even if I use USE_THOUSAND_SEPARATOR=True the {{ <big_number> }} is still rendered as 123456789 . But based on the documentation it should work, right? So going further in the documentation:

When set to True and USE_L10N is also True, Django will format numbers using the NUMBER_GROUPING and THOUSAND_SEPARATOR settings.

From here, I am a bit confused as it is not clear whether I can use the setting if USE_L10N = False. The sentence implies that I can use it in either case. I also set NUMBER_GROUPING = 3 and THOUSAND_SEPARATOR = "," as the documentation suggests. The {{ <big_number> }} is still rendered as 123456789 .

At this point I also try and set USE_L10N = True. The {{ <big_number> }} is now rendered as 123.456.789 . So the thousand separator now works, but with the wrong character. This makes sense because the locale I am using dictates . as the separator. However, the project requires , as the separator. Going back to the documentation:

These settings may also be dictated by the locale, which takes precedence.

So the behavior I am seeing is correct, but I am stuck, because if I remove the locale, the the separator disappears as well.

I am not sure what I am missing here, but is seems to me that the separator settings make sense only when the localization is False, since it takes precedence. But they don't seem to work at all.

I respect the decision of the maintainers to consider this as not an issue and I am just including the above in case they are useful (I am not expecting an answer).

comment:5 by Claude Paroz, 2 years ago

I feel your pain :-)
Django is currently in a transition period to remove the USE_L10N settings, which we hope will simplify a bit the settings combination hell. The downside is that it will make the life a bit harder for those wanting to force some specific locale formatting for their project. The documentation will also probably need some rework here and there, so we appreciate your input.

Note: See TracTickets for help on using tickets.
Back to Top