mirror of
https://github.com/django/django.git
synced 2025-01-03 06:55:47 +00:00
Fixed #34518 -- Fixed crash of random() template filter with an empty list.
This commit is contained in:
parent
18a7f2c711
commit
7d0e566208
@ -628,7 +628,10 @@ def length_is(value, arg):
|
||||
@register.filter(is_safe=True)
|
||||
def random(value):
|
||||
"""Return a random item from the list."""
|
||||
return random_module.choice(value)
|
||||
try:
|
||||
return random_module.choice(value)
|
||||
except IndexError:
|
||||
return ""
|
||||
|
||||
|
||||
@register.filter("slice", is_safe=True)
|
||||
|
@ -24,3 +24,8 @@ class RandomTests(SimpleTestCase):
|
||||
"random02", {"a": ["a&b", "a&b"], "b": [mark_safe("a&b"), mark_safe("a&b")]}
|
||||
)
|
||||
self.assertEqual(output, "a&b a&b")
|
||||
|
||||
@setup({"empty_list": "{{ list|random }}"})
|
||||
def test_empty_list(self):
|
||||
output = self.engine.render_to_string("empty_list", {"list": []})
|
||||
self.assertEqual(output, "")
|
||||
|
Loading…
Reference in New Issue
Block a user