Opened 19 months ago
Closed 19 months ago
#34518 closed Bug (fixed)
"random" template filter crashes on an empty list.
Reported by: | Алексей Поклонский | Owned by: | David Sanders |
---|---|---|---|
Component: | Template system | Version: | 4.0 |
Severity: | Normal | Keywords: | random templates |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
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.
Change History (9)
comment:1 by , 19 months ago
comment:2 by , 19 months ago
Keywords: | first last list removed |
---|---|
Summary: | `random` and `first`/`last` filters have different behavior on empty lists. → "random" template filter crashes on an empty list. |
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 19 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 19 months ago
Easy pickings: | set |
---|
comment:6 by , 19 months ago
Has patch: | set |
---|---|
Owner: | changed from | to
Sorry Aman I just submitted a PR… I didn't see you take ownership as my window was out-of-date.
comment:8 by , 19 months ago
Easy pickings: | set |
---|---|
Has patch: | set |
Triage Stage: | Accepted → Ready for checkin |
In addition, it is worth to underline that in official documentation there is no any notes about raising an error in
random
tag description.