Opened 2 hours ago

#36186 new Uncategorized

forloop should have an accessor for the total count of the loop

Reported by: Jonathan Ströbele Owned by:
Component: Template system Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The forloop has no way for accessing the total loop iteration count, though the length of the values being iterated is already known on the logic for building up the forloop variable.

I propose to expose this count on the forloop variable, i.e. like: forloop.total. I think it can be helpful to have this information available inside a loop if needed (this also the case for example in the Laravel/Blade loop variable):

{% for val in values %}
  {{ forloop.total }}
{% endfor %}

I prepared a patch here and can send it of as a PR if it's okay: https://github.com/stroebjo/django/commit/0a6233bcd9f2356a2b7b646ad4448ee313dc4d43

Change History (0)

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