Fixed #22294 -- Prevented converting length filter output to string
Thanks Steve Pike for the report.
This commit is contained in:
parent
6a0291bdaf
commit
bc315266c8
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
@ -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)
|
||||
|
|
|
@ -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…
Reference in New Issue