From d4dd3e503c88db92f254769a64b2fcd4c572c7dc Mon Sep 17 00:00:00 2001 From: mriduldhall Date: Fri, 25 Jul 2025 16:27:20 +0100 Subject: 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 --- django/template/defaultfilters.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'django') diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index b50b790fc1..3ed9856c08 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -432,7 +432,10 @@ def rjust(value, arg): @stringfilter def center(value, arg): """Center the value in a field of a given width.""" - return value.center(int(arg)) + width = int(arg) + if width <= 0: + return value + return f"{value:^{width}}" @register.filter -- cgit v1.3