﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34518	"""random"" template filter crashes on an empty list."	Алексей Поклонский	David Sanders	"`first`/`last` filters applied to a empty list do not raise anything and just returns a empty page. Example:

{{{
{{ list_var|last }}
}}}

Rendered with:

{{{
def index(request):
    return render(request, 'polls/index.html', context={""list_var"": []})
}}}

Will result in empty page:

{{{
[26/Apr/2023 09:15:49] ""GET / HTTP/1.1"" 200 0
}}}

As I undestand, 0 means no any characters in response content.

The similar situation is with the `first` tag:

{{{
{{ list_var|first }}
}}}

While the `random` tag raises the IndexError and causes the server to response 500.

Code snippet:

{{{
{{ list_var|random }}
}}}

In console we have:

{{{
Internal Server Error: /
Traceback (most recent call last):
  File ""/home/alex/.local/lib/python3.9/site-packages/django/core/handlers/exception.py"", line 47, in inner
    response = get_response(request)
  File ""/home/alex/.local/lib/python3.9/site-packages/django/core/handlers/base.py"", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File ""/home/alex/Документы/django-example/polls/views.py"", line 8, in index
    return render(request, 'polls/index.html', context={""list_var"": []})
  File ""/home/alex/.local/lib/python3.9/site-packages/django/shortcuts.py"", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/loader.py"", line 62, in render_to_string
    return template.render(context, request)
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/backends/django.py"", line 61, in render
    return self.template.render(context)
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/base.py"", line 176, in render
    return self._render(context)
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/base.py"", line 168, in _render
    return self.nodelist.render(context)
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/base.py"", line 977, in render
    return SafeString(''.join([
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/base.py"", line 978, in <listcomp>
    node.render_annotated(context) for node in self
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/base.py"", line 938, in render_annotated
    return self.render(context)
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/base.py"", line 1038, in render
    output = self.filter_expression.resolve(context)
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/base.py"", line 728, in resolve
    new_obj = func(obj, *arg_vals)
  File ""/home/alex/.local/lib/python3.9/site-packages/django/template/defaultfilters.py"", line 616, in random
    return random_module.choice(value)
  File ""/usr/lib/python3.9/random.py"", line 347, in choice
    return seq[self._randbelow(len(seq))]
IndexError: list index out of range
[26/Apr/2023 09:30:03] ""GET / HTTP/1.1"" 500 112925
}}}

Django version ''4.0.1'', python version Python ''3.9.2''

Expected behavior:

Since all `first`, `last` and `random` tags returns an element from the list, their conduct in the case when the list is empty must be the same: either all 3 of them should raise an error or all 3 should return empty content."	Bug	closed	Template system	4.0	Normal	fixed	random templates		Ready for checkin	1	0	0	0	1	0
