get rows count from django_tables2

3.7k views Asked by At

I'm new at django, and I need to get table row count (best it would be if before rendering table) of table which rendered with django table2 in my template

have some code like this:

{% load render_table from django_tables2 %} 
{% block content %}
    {% render_table  participations_table %}
{% endblock %}

and I want to render this table if there is at least 1 row in it.

1

There are 1 answers

1
Alasdair On BEST ANSWER

You can check if there are any rows using the table's rows attribute

{% if participations_table.rows %}
    {% render_table  participations_table %}
{% endif %}

In the django template, you can get the number of rows with the length filter.

{{ participations_table.rows|length }}

Or in the view, simply

len(participations_table.rows)

Alternatively, you could decide to always display the table, and customize the empty_text attribute which is displayed when the table is empty.