Search code examples
pythondjangodjango-templatesdjango-template-filters

Django template filter extract from list


My view context dictionary produces a list of footage numbers (eg. [1200, 1137, 1428, 5774... ]). I have corresponding tags in my template that pull specific list members...

<td> {{ footage | slice:"-1:" }}</td><td> {{ footage | slice:"-2:-1" }}</td><td> {{ footage | slice:"-3:-2" }}</td><td> {{ footage | slice:"-4:-3" }}</td>

problem is – when rendered, the html displays the values as a list with a single value – as opposed to a standalone string...

screen grab of rendered table

how can I extract the values from the brackets (pull them from the lists)?


Solution

  • You can simply do {{footage.0}}.

    Or in your example if you want to list them all in reverse order you can do:

    {% for f in footage reversed %}
     {{f}}
    {% endfor %}