Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django sorting table with pagination [closed]

Tags:

django

I want to sort my table columns both ways (ascending and descending, switch upon pressing a button). The problem I have is my tables go out of order when I switch a page.

views.py

def company_index(request):
    order_by = request.GET.get('order_by')
    companies = Company.objects.all().order_by(Lower(order_by))

    paginator = Paginator(companies, 10)
    page = request.GET.get('page')
    try:
        all_companies = paginator.page(page)
    except PageNotAnInteger:
        all_companies = paginator.page(1)
    except EmptyPage:
        all_companies = paginator.page(paginator.num_pages)

    return render(request, 'companies/company_index.html',
                  {'all_companies': all_companies})

Here is how I display data in my templates (I shortened class names for better post visibility):

<thead>
    <tr>
        <th>Company name <a class="glyphicon" href="?order_by=company_name"></a></th>
        <th>Company address <a class="glyphicon" href="?order_by=company_address"></a></th>
        <th>Tax ID <a class="glyphicon" href="?order_by=tax_id"></a></th>
        <th>Company ID <a class="glyphicon" href="?order_by=company_id"></a></th>
        <th>KRS Number <a class="glyphicon" href="?order_by=KRS_number"></a></th>
    </tr>
</thead>

My pagination code:

<ul class="pagination">
    {% if all_companies.has_previous %}
    <li><a href="?page={{ all_companies.previous_page_number }}&?order_by={{order_by}}">previous</a></li>
    {% endif %}
    <li class="disabled"><a>Page {{ all_companies.number }} of {{ all_companies.paginator.num_pages }}</a></li>
    {% if all_companies.has_next %}
    <li><a href="?page={{ all_companies.next_page_number }}&?order_by={{order_by}}">next</a></li>
    {% endif %}
</ul>

When I switch to other page {{order_by}} passes None. Also, how can I make it sort descending or ascending, after pressing a button?

I want to do it without outside apps or libraries, to have a better understanding of django.

like image 845
rockarou Avatar asked Nov 16 '25 20:11

rockarou


1 Answers

You forgot to add order_by in the context:

return render(request, 'companies/company_index.html',
              {'all_companies': all_companies, 'order_by': order_by})

Ascending/descending in template:

<a href="?order_by=company_name&direction=asc">company name ascending</a>
<a href="?order_by=company_name&direction=desc">company name descending</a>
...
<a href="?page={{ all_companies.previous_page_number }}&order_by={{ order_by }}&direction={{ direction }}">previous</a>
...
<a href="?page={{ all_companies.next_page_number }}&order_by={{ order_by }}&direction={{ direction }}">next</a>

In the view:

def company_index(request):
    order_by = request.GET.get('order_by')
    direction = request.GET.get('direction')
    ordering = Lower(order_by)
    if direction == 'desc':
        ordering = '-{}'.format(ordering)
    companies = Company.objects.all().order_by(ordering)

    paginator = Paginator(companies, 10)
    page = request.GET.get('page')
    try:
        all_companies = paginator.page(page)
    except PageNotAnInteger:
        all_companies = paginator.page(1)
    except EmptyPage:
        all_companies = paginator.page(paginator.num_pages)

    return render(request, 'companies/company_index.html',
                  {'all_companies': all_companies, 
                   'order_by': order_by, 'direction': direction})
like image 84
Anton Shurashov Avatar answered Nov 18 '25 14:11

Anton Shurashov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!