Opened 5 years ago

Closed 3 years ago

#30086 closed Cleanup/optimization (fixed)

Make floatformat template filter independent of USE_L10N and allow forcing unlocalized format.

Reported by: Meiyer Owned by: Mariusz Felisiak
Component: Template system Version: 1.11
Severity: Normal Keywords:
Cc: Florian Demmer, Carsten Fuchs Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

Specifically, when the filter is used within the {% localize [on|off] %} block with the localization setting opposed to the value of USE_L10N ('on' when USE_L10N = False or 'off' when USE_L10N = True), the localization setting has not effect.

This is due to the use of formats.number_format() without its use_l10n parameter, by the numberformat template filter (e.g. https://github.com/django/django/blob/c2c85663e2dd06c9ed9c9ec2d02202d6d668d7f0/django/template/defaultfilters.py#L144, https://github.com/django/django/blob/c2c85663e2dd06c9ed9c9ec2d02202d6d668d7f0/django/template/defaultfilters.py#L163). The value of the use_l10n parameter shall be taken out of the template rendering context. But I do not see any easy solution to this, as filters do not take context...

Change History (15)

comment:1 by Tim Graham, 5 years ago

Component: Template systemDocumentation
Description: modified (diff)
Summary: The numberformat template filter does not respect local localization settingsDocument how floatformat template filter interacts with the localize template tag
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

As long as the behavior doesn't contract existing documentation, perhaps the solution is the document the current behavior.

in reply to:  1 ; comment:2 by Marc Parizeau, 5 years ago

Replying to Tim Graham:

As long as the behavior doesn't contract existing documentation, perhaps the solution is the document the current behavior.

Documenting would be better than nothing, but this behavior goes against the principle of least astonishment. Finding a way to honor the localize directive would be better. Any other filters that are affected by this limitation?

comment:3 by Claude Paroz, 5 years ago

All filters are affected. As the reporter noticed, the filters are currently not aware of the rendering context. That's no easy fix.

in reply to:  2 comment:4 by Meiyer, 5 years ago

Replying to Marc Parizeau:

this behavior goes against the principle of least astonishment

+1
I consider this as a bug in the implementation of the filter, even though the original title of the ticket was modified by Tim. Why would something inside a {% localize off %} block still keep being localised?.. I actually opened this ticket after having to dig through Django's code and adding debug statements to understand what was happening.

Going forward, I see only two possible solutions:

  • either introduce for filters a non-breaking support for context (meaning, filters will be able to take context without breaking existing code),
  • or, deprecate the floatformat filter (and possibly other), replacing it (them) with tags.

I personally favour the first option, but I realise it will require a lot of effort.

comment:5 by Tim Graham, 5 years ago

In my 10 years of work with Django, I don't recall another report of this issue, so I doubt that such disruptive changes would be acceptable for what I perceive as a relatively minor issue. You're welcome to make your proposal on the DevelopersMailingList to try to gather a consensus. A custom floatformat template tag may meet your needs, but deprecating the filter seems a bit drastic for the 99% of users who aren't affected by this issue.

comment:6 by Claude Paroz, 5 years ago

Did someone explore adding support for @register.filter(takes_context=True). If someone can produce some proof-of-concept code, it may have a chance to get accepted.

comment:7 by Florian Demmer, 4 years ago

Cc: Florian Demmer added

comment:8 by Carsten Fuchs, 3 years ago

Cc: Carsten Fuchs added

comment:9 by Mariusz Felisiak, 3 years ago

Resolution: duplicate
Status: newclosed
Triage Stage: AcceptedUnreviewed

Closing as a duplicate of #32873 — which will remove the USE_L10N setting.

comment:10 by Meiyer, 3 years ago

Mariusz, I do not see how Claude’s PR resolves the problem discussed in this ticket. The floatformat builtin filter will still return a localised result irrespective of the {% localize off %} tag. This is not yet documented in https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#floatformat.

in reply to:  10 comment:11 by Mariusz Felisiak, 3 years ago

Resolution: duplicate
Status: closednew
Triage Stage: UnreviewedAccepted

Replying to Meiyer:

Mariusz, I do not see how Claude’s PR resolves the problem discussed in this ticket. The floatformat builtin filter will still return a localised result irrespective of the {% localize off %} tag. This is not yet documented in https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#floatformat.

True, we can still document that floatformat ignores {% localize on|off %}.

comment:12 by Mariusz Felisiak, 3 years ago

Has patch: set
Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:13 by Mariusz Felisiak, 3 years ago

Component: DocumentationTemplate system
Summary: Document how floatformat template filter interacts with the localize template tagMake floatformat template filter independent of USE_L10N and allow forcing unlocalized format.

comment:14 by Carlton Gibson, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:15 by GitHub <noreply@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 4a43335d:

Fixed #30086, Refs #32873 -- Made floatformat template filter independent of USE_L10N.

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