publik: fix |first & |last on KeyError (#70954)

This commit is contained in:
Lauréline Guérin 2022-11-03 10:00:18 +01:00
parent df0a449167
commit a49ab97480
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 8 additions and 2 deletions

View File

@ -63,7 +63,7 @@ def split(string, separator=' '):
def first(value):
try:
return defaultfilters.first(value)
except TypeError:
except (TypeError, KeyError):
return ''
@ -71,7 +71,7 @@ def first(value):
def last(value):
try:
return defaultfilters.last(value)
except TypeError:
except (TypeError, KeyError):
return ''

View File

@ -78,6 +78,9 @@ def test_first():
context = Context({'foo': None})
assert t.render(context) == ''
context = Context({'foo': {}})
assert t.render(context) == ''
def test_last():
t = Template('{{ foo|last }}')
@ -94,6 +97,9 @@ def test_last():
context = Context({'foo': None})
assert t.render(context) == ''
context = Context({'foo': {}})
assert t.render(context) == ''
def test_decimal():
tmpl = Template('{{ plop|decimal }}')