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 , 2 months ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 2 months ago
Easy pickings: | set |
---|
comment:3 by , 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:5 by , 2 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
Thank you!
I will make sure to provide regular updates on any progress/issues
comment:6 by , 2 months ago
Has patch: | set |
---|
comment:8 by , 2 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:9 by , 2 months ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
comment:10 by , 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 , 2 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:12 by , 2 months ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
comment:13 by , 2 months ago
Patch needs improvement: | unset |
---|
comment:14 by , 2 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
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.)