From a49ab97480df7332436090428cba6222bb522b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Thu, 3 Nov 2022 10:00:18 +0100 Subject: [PATCH] publik: fix |first & |last on KeyError (#70954) --- publik_django_templatetags/publik/templatetags/publik.py | 4 ++-- tests/test_publik.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/publik_django_templatetags/publik/templatetags/publik.py b/publik_django_templatetags/publik/templatetags/publik.py index 26932bc..07eaaf3 100644 --- a/publik_django_templatetags/publik/templatetags/publik.py +++ b/publik_django_templatetags/publik/templatetags/publik.py @@ -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 '' diff --git a/tests/test_publik.py b/tests/test_publik.py index 8904b16..c34a60e 100644 --- a/tests/test_publik.py +++ b/tests/test_publik.py @@ -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 }}')