Loading django/template/defaultfilters.py +1 −1 Original line number Diff line number Diff line Loading @@ -572,7 +572,7 @@ def last(value): return '' @register.filter(is_safe=True) @register.filter(is_safe=False) def length(value): """Returns the length of the value - useful for lists.""" try: Loading docs/ref/templates/builtins.txt +4 −2 Original line number Diff line number Diff line Loading @@ -1680,7 +1680,8 @@ For example:: {{ value|length }} If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``4``. If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be ``4``. .. templatefilter:: length_is Loading @@ -1693,7 +1694,8 @@ For example:: {{ value|length_is:"4" }} If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``True``. If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be ``True``. .. templatefilter:: linebreaks Loading tests/defaultfilters/tests.py +2 −1 Original line number Diff line number Diff line Loading @@ -20,7 +20,7 @@ from django.test import TestCase from django.utils import six from django.utils import translation from django.utils.encoding import python_2_unicode_compatible from django.utils.safestring import SafeData from django.utils.safestring import mark_safe, SafeData class DefaultFiltersTests(TestCase): Loading Loading @@ -495,6 +495,7 @@ class DefaultFiltersTests(TestCase): def test_length(self): self.assertEqual(length('1234'), 4) self.assertEqual(length(mark_safe('1234')), 4) self.assertEqual(length([1, 2, 3, 4]), 4) self.assertEqual(length_is([], 0), True) self.assertEqual(length_is([], 1), False) Loading tests/template_tests/filters.py +3 −2 Original line number Diff line number Diff line Loading @@ -320,9 +320,10 @@ def get_filter_tests(): 'length02': ('{{ list|length }}', {'list': []}, '0'), 'length03': ('{{ string|length }}', {'string': ''}, '0'), 'length04': ('{{ string|length }}', {'string': 'django'}, '6'), 'length05': ('{% if string|length == 6 %}Pass{% endif %}', {'string': mark_safe('django')}, 'Pass'), # Invalid uses that should fail silently. 'length05': ('{{ int|length }}', {'int': 7}, ''), 'length06': ('{{ None|length }}', {'None': None}, ''), 'length06': ('{{ int|length }}', {'int': 7}, ''), 'length07': ('{{ None|length }}', {'None': None}, ''), # length_is filter. 'length_is01': ('{% if some_list|length_is:"4" %}Four{% endif %}', {'some_list': ['4', None, True, {}]}, 'Four'), Loading Loading
django/template/defaultfilters.py +1 −1 Original line number Diff line number Diff line Loading @@ -572,7 +572,7 @@ def last(value): return '' @register.filter(is_safe=True) @register.filter(is_safe=False) def length(value): """Returns the length of the value - useful for lists.""" try: Loading
docs/ref/templates/builtins.txt +4 −2 Original line number Diff line number Diff line Loading @@ -1680,7 +1680,8 @@ For example:: {{ value|length }} If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``4``. If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be ``4``. .. templatefilter:: length_is Loading @@ -1693,7 +1694,8 @@ For example:: {{ value|length_is:"4" }} If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``True``. If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be ``True``. .. templatefilter:: linebreaks Loading
tests/defaultfilters/tests.py +2 −1 Original line number Diff line number Diff line Loading @@ -20,7 +20,7 @@ from django.test import TestCase from django.utils import six from django.utils import translation from django.utils.encoding import python_2_unicode_compatible from django.utils.safestring import SafeData from django.utils.safestring import mark_safe, SafeData class DefaultFiltersTests(TestCase): Loading Loading @@ -495,6 +495,7 @@ class DefaultFiltersTests(TestCase): def test_length(self): self.assertEqual(length('1234'), 4) self.assertEqual(length(mark_safe('1234')), 4) self.assertEqual(length([1, 2, 3, 4]), 4) self.assertEqual(length_is([], 0), True) self.assertEqual(length_is([], 1), False) Loading
tests/template_tests/filters.py +3 −2 Original line number Diff line number Diff line Loading @@ -320,9 +320,10 @@ def get_filter_tests(): 'length02': ('{{ list|length }}', {'list': []}, '0'), 'length03': ('{{ string|length }}', {'string': ''}, '0'), 'length04': ('{{ string|length }}', {'string': 'django'}, '6'), 'length05': ('{% if string|length == 6 %}Pass{% endif %}', {'string': mark_safe('django')}, 'Pass'), # Invalid uses that should fail silently. 'length05': ('{{ int|length }}', {'int': 7}, ''), 'length06': ('{{ None|length }}', {'None': None}, ''), 'length06': ('{{ int|length }}', {'int': 7}, ''), 'length07': ('{{ None|length }}', {'None': None}, ''), # length_is filter. 'length_is01': ('{% if some_list|length_is:"4" %}Four{% endif %}', {'some_list': ['4', None, True, {}]}, 'Four'), Loading