[Django] #34518: `random` and `first`/`last` filters have different behavior on empty lists.

6 views
Skip to first unread message

Django

unread,
Apr 26, 2023, 5:39:13 AM4/26/23
to django-...@googlegroups.com
#34518: `random` and `first`/`last` filters have different behavior on empty lists.
-------------------------------------+-------------------------------------
Reporter: Алексей | Owner: nobody
Поклонский |
Type: Bug | Status: new
Component: Template | Version: 4.0
system | Keywords: random, first,
Severity: Normal | last, list, templates
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
`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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34518>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 26, 2023, 5:43:12 AM4/26/23
to django-...@googlegroups.com
#34518: `random` and `first`/`last` filters have different behavior on empty lists.
-------------------------------------+-------------------------------------
Reporter: Алексей Поклонский | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 4.0
Severity: Normal | Resolution:
Keywords: random, first, | Triage Stage:
last, list, templates | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Алексей Поклонский):

In addition, it is worth to underline that in
[https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#random
official documentation] there is no any notes about raising an error in
`random` tag description.

--
Ticket URL: <https://code.djangoproject.com/ticket/34518#comment:1>

Reply all
Reply to author
Forward
0 new messages