[PYTHON] Loop the For statement in reverse in an HTML file on Django

When creating a blog-type Web application, I thought about creating a view that can display posts as a list using ListView. I tried to display it using the For statement, but as expected, it was sorted in order from the oldest post, so I want to sort it in the newest order. (I want to change the display method from ascending order to descending order)

I thought about putting a list in the reverse order in a variable and turning it with a For statement, but I wrote it using the simpler for statement reversed.

list.html


   {% for i in object_list reversed %}
        <div class="post-preview">
          <a href="{% url 'post' i.pk %}">
            <h2 class="post-title">{{ i.title }}</h2>
            <h3 class="post-subtitle">{{ i.subtitle }}</h3>
          </a>
        </div>
   {% endfor %}

By the way {% for i in reversed(object_list) %} I also found out how to write it, but I got an error. Maybe it's because there is a unique `{%%}` when writing Python in HTML.

So for the time being, I think `{% for i in object_list reversed%}` is correct.

Recommended Posts

Loop the For statement in reverse in an HTML file on Django
Change the list in a for statement
Do an ambiguous search for mysql in Django
Django template reads Makdown and reStructuredText written in an external file as HTML
Learning notes for the migrations feature in the Django framework (2)
Double loop in for statement and then print statement behavior
Learning notes for the migrations feature in the Django framework (3)
Make the model a string on a Django HTML template
Learning notes for the migrations feature in the Django framework (1)
Django returns the contents of the file as an HTTP response
Summary of stumbling blocks in Django for the first time
Google search for the last line of the file in Python
Download the file in Python
Check if the configuration file is read in an easy-to-understand manner
[Introduction to Python] How to use the in operator in a for statement?
[Django] How to read variables / constants defined in an external file
Get the formula in an excel file as a string in Python
Try Ajax on the Django page
Switch the language displayed in Django 1.9
The story of the "hole" in the file
The meaning of ".object" in Django
Start Django for the first time
Get the query string (query string) in Django
Create an empty array in Numpy to add rows for each loop
Open an Excel file in Python and color the map of Japan
How to save the feature point information of an image in a file and use it for matching