[Fixed]-Django reverse order of ascending/descending list

1👍

Use addresses like /books/?order=desc and /books/?order=asc

And in your view handle this flag:

def book_list_title(request):
    order = request.GET.get('order', 'desc')

    all_entries = Book.objects.all()

    if(order == 'desc'):
        all_entries = all_entries.order_by('-title')

    elif(order == 'asc'):
        all_entries = all_entries.order_by('title')

You can also pass order variable into template and depends on it show direction of order in the link

{% if order == 'desc' %}/books/?order=asc{% else %}/books/?order=desc{% endif %}

Leave a comment