Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django tables how to detect if table is empty

I am new to django and web development and based on examples and help on SO, I have pieced together something which takes a model and renders it in a django-table. My template code is basically as follows:

{% block content %}
{% load static %}
{% load render_table from django_tables2 %}
 <div class="function-page">
     <div class="table-form">
        <div class="function-container">
                {% render_table reviews %}
        </div>
     </div>
 </div>
{% endblock %}

The view is as follows:

@login_required(login_url="login/")
def review(request):
    table = DummyTable(DummyModel.objects.all())
    form = DummyForm()
    RequestConfig(request, paginate={"per_page": 10}).configure(table)
    return render(request, 'review.html', {'reviews': table, 'DummyForm': form})

This works fine. However, what I would like to do is show a message to the user saying that there are no records when the database table is empty. In the current setting, it shows an empty table with the columns which is probably not the best from a usability point of view.

like image 580
Luca Avatar asked Dec 10 '22 12:12

Luca


2 Answers

There are two options. Either you set empty_text inside class Meta

class Reviews(tables.Table):

    class Meta:
        empty_text = _("There are no reviews yet")

Or you can check it inside the template and avoid rendering table this way

{% if reviews_table.data.list %}
    {% render_table reviews_table %}
{% else %}
    <h1>There are no reviews yet</h1>
{% endif %}
like image 81
Milano Avatar answered Feb 04 '23 19:02

Milano


Probably the easiest way is in your template. Assuming your variable that's empty is called reviews:

{% block content %}
{% load static %}
{% if reviews %} 
   {% load render_table from django_tables2 %}
      <div class="function-page">
         <div class="table-form">
            <div class="function-container">
                {% render_table reviews %}
           </div>
         </div>
       </div>
{% else %} 
<span> Whatever holding response/error message you want. </span> 
{% endif %}
{% endblock %}

Per this this answer, for example, using {% if variable %} against a valid but empty variable, it generally evaluates to False, letting you use the {% if reviews %}.

However, if you need a really bulletproof check, you can do {{ value|default:"nothing" }} - from here.

You could also do it in your views, and pass an error message back to the template using the standard Messages framework included in Django:

from django.contrib import messages
messages.add_message(request, messages.INFO, "No reviews found, I'm afraid!.")

You need to include something like this in your templates to use messages:

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

Or you could do both! :)

like image 31
Withnail Avatar answered Feb 04 '23 17:02

Withnail