[Fixed]-Sort Django objects by attribute in template

1👍

instead of a button use a link and pass a parameter to the view:

<th><a href="{{ url('myurl') }}?sort=name">Name </a></th>

in the django view you can retrieve the sort parameter using : request.GET.get('sort', 'your_default_sort') and use it to order the queryset. If you’re using FBV change it manually in the function, and if you’re using CBV just override the get_ordering() method of your class

however, It would me much much easier with a client-side solution like datatables

Hope this helps

Leave a comment