Search code examples
djangodjango-tables2

Right number of rows, but they're empty


I'm trying to display some tables, but they come up empty. The part that gets me is the number of rows is correct, but they are completely blank. If the table has 9 entries, I get 9 empty rows.

The same code is working for a different table

tables.py:

class VouchersTable(tables.Table):
    class meta:
        model = Vouchers
        fields = ('event_name', 'pk', 'valid_start', 'valid_end', 'lab_duration', 'user_email', 'redeem_date' )

views.py:

class ReportsView(LoginRequiredMixin, TemplateView):
    template_name = 'reports.html'

    def get_context_data(self, **kwargs):
        context = super(ReportsView, self).get_context_data(**kwargs)
        vouchers = VouchersTable(Vouchers.objects.all())
        RequestConfig(self.request, paginate=False).configure(vouchers)
        context['vouchers'] = vouchers

        return context

reports.html:

{% extends "base.html" %}
{% load render_table from django_tables2 %}

{% block content %}
  {% render_table vouchers %}
{% endblock content %}

models.py:

class Vouchers(models.Model):
    creator_uid = models.IntegerField()
    user_id = models.IntegerField()
    username = models.CharField(max_length=255)
    user_email = models.CharField(max_length=100)
    event_name = models.CharField(max_length=255)
    event_code = models.IntegerField()
    valid_start = models.IntegerField()
    valid_end = models.IntegerField()
    redeemed = models.IntegerField()
    redeem_date = models.IntegerField()
    lab_version = models.CharField(max_length=40)
    lab_model = models.IntegerField()
    lab_id = models.IntegerField()
    lab_duration = models.IntegerField()

resulting html (empty lines removed):

<div class="table-container">
<table>
    <thead>
        <tr>
        </tr>
    </thead>
    <tbody> 
        <tr class="even">
        </tr>
        <tr class="odd">
        </tr>
        <tr class="even">
        </tr>
        <tr class="odd">
        </tr>
        <tr class="even">
        </tr>
        <tr class="odd">
        </tr>
        <tr class="even">
        </tr>
        <tr class="odd">
        </tr>
        <tr class="even">
        </tr>
    </tbody>
</table>
</div>

Solution

  • Oh well... You've declared the table like this:

    class VouchersTable(tables.Table):
        class meta:
            model = Vouchers
            fields = ('event_name', 'pk', 'valid_start', 'valid_end', 'lab_duration', 'user_email', 'redeem_date' )
    

    The correct way to write it is class Meta (Meta with a capital M): https://github.com/jieter/django-tables2