mirror of
https://github.com/django/django.git
synced 2025-09-03 02:45:09 +03:00
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@lilyf.org>
This commit is contained in:
parent
2d4ca62170
commit
d4dd3e503c
1
AUTHORS
1
AUTHORS
|
@ -764,6 +764,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Morgan Aubert <morgan.aubert@zoho.com>
|
Morgan Aubert <morgan.aubert@zoho.com>
|
||||||
Moritz Sichert <moritz.sichert@googlemail.com>
|
Moritz Sichert <moritz.sichert@googlemail.com>
|
||||||
Morten Bagai <m@bagai.com>
|
Morten Bagai <m@bagai.com>
|
||||||
|
Mridul Dhall <mriduldhall1@gmail.com>
|
||||||
msaelices <msaelices@gmail.com>
|
msaelices <msaelices@gmail.com>
|
||||||
msundstr
|
msundstr
|
||||||
Mushtaq Ali <mushtaak@gmail.com>
|
Mushtaq Ali <mushtaak@gmail.com>
|
||||||
|
|
|
@ -432,7 +432,10 @@ def rjust(value, arg):
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def center(value, arg):
|
def center(value, arg):
|
||||||
"""Center the value in a field of a given width."""
|
"""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
|
@register.filter
|
||||||
|
|
|
@ -35,6 +35,12 @@ class FunctionTests(SimpleTestCase):
|
||||||
def test_non_string_input(self):
|
def test_non_string_input(self):
|
||||||
self.assertEqual(center(123, 5), " 123 ")
|
self.assertEqual(center(123, 5), " 123 ")
|
||||||
|
|
||||||
|
def test_odd_input(self):
|
||||||
|
self.assertEqual(center("odd", 6), " odd ")
|
||||||
|
|
||||||
|
def test_even_input(self):
|
||||||
|
self.assertEqual(center("even", 7), " even ")
|
||||||
|
|
||||||
def test_widths(self):
|
def test_widths(self):
|
||||||
value = "something"
|
value = "something"
|
||||||
for i in range(-1, len(value) + 1):
|
for i in range(-1, len(value) + 1):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user