What is solution for clicking sorting order by on landing page

Hi,

I would like this page to be able to sort orders on the table.
Just guide reference document or sample code to code on view.py.

I use function base for coding in view.py.

I see any table view in the admin section, they can be sorted in the table view.

this is a simple code in view.py.

def list_transaction(request):

    all_t = JobTransaction.objects.all()

    context = {'TransList':all_t}
    return render(request, 'app/cbw_list_transaction.html', context)

If you look at how the Django admin does this, you’ll see that each header is a link back to the same page, but with a parameter added to the URL. That parameter identifies which column to use for the sort. If you want to do it this same way, then you’d need to do something similar.

On one of my admin pages:

<th scope="col" class="sortable column-contract_modification">
   <div class="text"><a href="?o=3">Contract modification</a></div>
   <div class="clear"></div>
</th>

Sorry to bother you.
I think I understand what you recommend. I see in the admin page and it is the same as you mentioned.

but I try doing it on this page (outside the admin section).

it is not okay as my expectation. it can’t be sorted at all.

Setting the query variable is only half of it - you still need to modify your views to accept those parameters and use it to sort your queryset before being rendered.

I found a good solution to do in this case.
I use this package, It is ok for me and it is very easy to configure too.
https://pypi.org/project/webstack-django-sorting/

it work with https://pypi.org/project/django-filter/ greatly .
take a look below.

Main problem is that
The it is cannot use togather with pagination using-paginator-in-a-view-function

Once I add this part into the code, it cannot be sorted anymore.
In conclusion, we can not use filter, sorting, and paginator.
but you can apply either filter +paginator OR filter +sorting.

I suppose that webstack-django-sorting does not support paginator

Any idea to be able to use filter +paginator +sorting ?

Thank you
Pongthorn

Actually, we’ve gone a different route - we use jQuery with Datatables and django-datatables-view to handle all our tabular data displays.

1 Like

Thank you for different solution.
I read as your recommendation. it is good too.
Instead, It is rather difficult to do for me at the moment,I have to be in a hurry to complete this task.
I am determined to use webstack-django-sorting first,
if I have more time I will try django-datatables-view .

Appreciate your help all along.
Pongthorn