loop backwards using django template

PythonDjangoDjango Templates

Python Problem Overview


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!

Python Solutions


Solution 1 - Python

Directly from the page you linked:

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

Solution 2 - Python

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

{% for obj in list | reverse %}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionstill.LearningView Question on Stackoverflow
Solution 1 - PythonJoran BeasleyView Answer on Stackoverflow
Solution 2 - PythonKhizer NaeemView Answer on Stackoverflow