#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 Алексей Поклонский, 17 months ago

In addition, it is worth to underline that in official documentation there is no any notes about raising an error in random tag description.

comment:2 by Mariusz Felisiak, 17 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: UnreviewedAccepted

comment:3 by David Sanders, 17 months ago

Thanks for the report 👍

I think random should behave gracefully.

comment:4 by Aman Pandey, 17 months ago

Owner: changed from nobody to Aman Pandey
Status: newassigned

comment:5 by Mariusz Felisiak, 17 months ago

Easy pickings: set

comment:6 by David Sanders, 17 months ago

Has patch: set
Owner: changed from Aman Pandey to David Sanders

Sorry Aman I just submitted a PR… I didn't see you take ownership as my window was out-of-date.

(I promise I'll leave the next easy picking to you :))

Last edited 17 months ago by David Sanders (previous) (diff)

comment:7 by Aman Pandey, 17 months ago

Easy pickings: unset
Has patch: unset

It's totally fine : )

comment:8 by Mariusz Felisiak, 17 months ago

Easy pickings: set
Has patch: set
Triage Stage: AcceptedReady for checkin

comment:9 by GitHub <noreply@…>, 17 months ago

Resolution: fixed
Status: assignedclosed

In 7d0e566:

Fixed #34518 -- Fixed crash of random() template filter with an empty list.

Note: See TracTickets for help on using tickets.
Back to Top