56

I have a list of objects and I am trying to display them all (and so I am using the django {% for %} {% endfor %}) However, I need to iterate through each object backwards one at a time, rather than forwards. I've looked at https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for but I couldn't really figure out how I can use it to loop backwards. I was wondering how to do this and if it is even possible. Below is a simple example of how I currently have it implemented (iterating forward):

...
{% for i in scheduling_info %}
    <pre>{{ i.log }}</pre>
{% endfor %}
...

Thanks!

1
  • 4
    not sure why you got -1 ... I +1'd to compensate as it is a valid question that others may find useful Commented Oct 1, 2012 at 20:44

2 Answers 2

144

Directly from the page you linked:

You can loop over a list in reverse by using {% for obj in list reversed %}.

0
18

In case someone ends up here looking for jinja2 solution, like me:

{% for obj in list | reverse %}
1
  • 5
    throw error: django.template.exceptions.TemplateSyntaxError: 'for' statements should use the format 'for x in y': for entry in entries | reverse
    – Wizard
    Commented May 21, 2018 at 11:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.