Opened 2 months ago

Closed 8 weeks ago

#36519 closed Bug (fixed)

The center template filter is inconsistent when adding an odd amount of padding

Reported by: Lily Acorn Owned by: Mridul
Component: Template system Version: 5.2
Severity: Normal Keywords: center filter
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Django supports a template filter for centering strings: {{ "Django"|center:10 }} which adds padding around the string to reach the provided length. In this simple case, the template is rendered as ··Django·· (where · represents a space character).

Things get weird when the total amount of padding required is an odd number. For example, {{ "odd"|center:6 }} becomes ·odd··, but {{ "even"|center:7 }} becomes ··even·. Note that the side with the extra space character is inconsistent.

The cause of this quirk is that Django’s center filter directly calls Python’s str.center method, which maintains this behaviour for backwards compatibilty reasons.

Python also supports centering via f-strings (and str.format):

>>> f"{'odd':^6}"
'·odd··'
>>> f"{'even':^7}"
'·even··'

and this behaves consistently - always adding the extra unpaired space on the right.

I think Django should match the f-string behaviour here.

Change History (15)

comment:1 by Carlton Gibson, 2 months ago

Triage Stage: UnreviewedAccepted

This seems right to me. The use case for these filters is aligning columns in plain text output. The inconsistency here is contrary to that.

(I realise in my own usage left/right are much more common than center, and even that is quite rare, which might account for this not being caught before.)

comment:2 by Lily Acorn, 2 months ago

Easy pickings: set

comment:3 by Mridul, 2 months ago

Hi,
I am new to the Django contribution community and would love to start contributing. I have been reading the available guides and this seems like a great issue for me to start on. Would it be ok for me to claim this ticket and work on it?
I am more than willing to learn from any feedback I receive.
Thanks

comment:4 by Lily Acorn, 2 months ago

Yes, go for it!

comment:5 by Mridul, 2 months ago

Owner: set to Mridul
Status: newassigned

Thank you!
I will make sure to provide regular updates on any progress/issues

comment:6 by Mridul, 2 months ago

Has patch: set

comment:8 by Lily Acorn, 2 months ago

Triage Stage: AcceptedReady for checkin

comment:9 by Natalia Bidart, 2 months ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

comment:10 by Mridul, 2 months ago

Patch needs improvement: unset

I have pushed out improvements based on the PR review. Would be more than happy to receive any further feedback

comment:11 by Lily Acorn, 2 months ago

Triage Stage: AcceptedReady for checkin

comment:12 by Natalia Bidart, 2 months ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

comment:13 by Mridul, 2 months ago

Patch needs improvement: unset

comment:14 by Lily Acorn, 2 months ago

Triage Stage: AcceptedReady for checkin

comment:15 by nessita <124304+nessita@…>, 8 weeks ago

Resolution: fixed
Status: assignedclosed

In d4dd3e50:

Fixed #36519 -- Made center template filter consistent for even/odd padding.

Refactored center template filter to match f-string behaviour,
producing consistent padding for both odd and even fillings.

Thanks Lily Acorn for the report and Natalia Bidart for the review.

Co-authored-by: Lily Acorn <code@…>

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