I have implemented ordering in a generic ListView:
class CarList(LoginRequiredMixin, ListView):
model = Car
paginate_by = 30
ordering = 'car_id_internal'
def get_ordering(self):
return self.request.GET.get('ordering', 'car_id_internal')
def get_context_data(self, *args, **kwargs):
context = super(CarList, self).get_context_data(*args, **kwargs)
context['current_order'] = self.get_ordering()
return context
And in my template:
<thead>
<tr>
<th><a href="{% url 'car_list' %}?ordering=car_id_internal">Internal car ID</a></th>
<th><a href="{% url 'car_list' %}?ordering=type">Type</a></th>
<th><a href="{% url 'car_list' %}?ordering=brand">Brand</a></th>
</tr>
</thead>
This works fine, however I would like for user to be able to reverse the order from ascending to descending when they click on the column header again. Is that possible with Django? I'm using Django 1.9.
I hope someone can help.