[Django]-Django reverse ordering with ListView

6👍

You need to use another variable for determining the asc/desc ordering. Depending on which is currently selected the template should alternate the links. In the view, you reverse the ordering by adding a dash before the field’s name. Try the following:

class CarList(ListView):
    model = Car
    paginate_by = 30

    ordering = 'car_id_internal'
    def get_ordering(self):
        self.order = self.request.GET.get('order', 'asc')
        selected_ordering = self.request.GET.get('ordering', 'car_id_internal')
        if self.order == "desc":
            selected_ordering = "-" + selected_ordering
        return selected_ordering

    def get_context_data(self, *args, **kwargs):
        context = super(CarList, self).get_context_data(*args, **kwargs)
        context['current_order'] = self.get_ordering()
        context['order'] = self.order
        return context

And the template:

<table>
<tr>
    <th><a href="{% url 'car_list' %}?ordering=car_id_internal&order={% if order == 'desc' %}asc{% else %}desc{% endif %}">Internal car ID</a></th>
    <th><a href="{% url 'car_list' %}?ordering=type&order={% if order == 'desc' %}asc{% else %}desc{% endif %}">Type</a></th>
    <th><a href="{% url 'car_list' %}?ordering=brand&order={% if order == 'desc' %}asc{% else %}desc{% endif %}">Brand</a></th>
</tr>
{% for car in object_list %}
<tr>
    <td>{{car.id}}</td>
    <td>{{car.type}}</td>
    <td>{{car.brand}}</td>
</tr>
{% endfor %}

👤NS0

Leave a comment