[Django] #36519: The center template filter is inconsistent when adding an odd amount of padding

19 views
Skip to first unread message

Django

unread,
Jul 22, 2025, 1:04:45 PMJul 22
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
-------------------------------+-------------------------------------------
Reporter: Lily Acorn | Type: Bug
Status: new | Component: Template system
Version: 5.2 | Severity: Normal
Keywords: center filter | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-------------------------------------------
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,
[https://github.com/python/cpython/issues/67812 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/36519>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 22, 2025, 2:29:03 PMJul 22
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: (none)
Type: Bug | Status: new
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Carlton Gibson):

* stage: Unreviewed => Accepted

Comment:

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.)
--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:1>

Django

unread,
Jul 22, 2025, 2:30:48 PMJul 22
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: (none)
Type: Bug | Status: new
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Lily Acorn):

* easy: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:2>

Django

unread,
Jul 22, 2025, 6:32:46 PMJul 22
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: (none)
Type: Bug | Status: new
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Comment (by Mridul):

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
--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:3>

Django

unread,
Jul 22, 2025, 6:33:26 PMJul 22
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: (none)
Type: Bug | Status: new
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Comment (by Lily Acorn):

Yes, go for it!
--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:4>

Django

unread,
Jul 22, 2025, 6:34:36 PMJul 22
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Mridul):

* owner: (none) => Mridul
* status: new => assigned

Comment:

Thank you!
I will make sure to provide regular updates on any progress/issues
--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:5>

Django

unread,
Jul 23, 2025, 9:53:15 AMJul 23
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Mridul):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:6>

Django

unread,
Jul 23, 2025, 9:57:58 AMJul 23
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Comment (by Mridul):

PR[https://github.com/django/django/pull/19669]
--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:7>

Django

unread,
Jul 23, 2025, 1:15:20 PMJul 23
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
-------------------------------------+-------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Lily Acorn):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:8>

Django

unread,
Jul 23, 2025, 1:18:14 PMJul 23
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Natalia Bidart):

* needs_better_patch: 0 => 1
* stage: Ready for checkin => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:9>

Django

unread,
Jul 24, 2025, 2:16:44 PMJul 24
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Mridul):

* needs_better_patch: 1 => 0

Comment:

I have pushed out improvements based on the PR review. Would be more than
happy to receive any further feedback
--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:10>

Django

unread,
Jul 24, 2025, 3:54:44 PMJul 24
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
-------------------------------------+-------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Lily Acorn):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:11>

Django

unread,
Jul 25, 2025, 8:28:08 AMJul 25
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Natalia Bidart):

* needs_better_patch: 0 => 1
* stage: Ready for checkin => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:12>

Django

unread,
Jul 25, 2025, 11:38:51 AMJul 25
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
---------------------------------+------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Mridul):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:13>

Django

unread,
Jul 25, 2025, 11:57:51 AMJul 25
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
-------------------------------------+-------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: assigned
Component: Template system | Version: 5.2
Severity: Normal | Resolution:
Keywords: center filter | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Lily Acorn):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:14>

Django

unread,
Jul 28, 2025, 3:41:18 PMJul 28
to django-...@googlegroups.com
#36519: The center template filter is inconsistent when adding an odd amount of
padding
-------------------------------------+-------------------------------------
Reporter: Lily Acorn | Owner: Mridul
Type: Bug | Status: closed
Component: Template system | Version: 5.2
Severity: Normal | Resolution: fixed
Keywords: center filter | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nessita <124304+nessita@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"d4dd3e503c88db92f254769a64b2fcd4c572c7dc" d4dd3e50]:
{{{#!CommitTicketReference repository=""
revision="d4dd3e503c88db92f254769a64b2fcd4c572c7dc"
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 <co...@lilyf.org>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36519#comment:15>
Reply all
Reply to author
Forward
0 new messages