Search code examples
djangodjango-viewsdjango-tables2

how do i create a table from a dict using django-tables2


I have a list of dict that look like this:

 [set([u'meal', '0:08:35.882945']),
  set([0, u'personal']),
  set([0, u'sleep']),
  set([0, u'transport']),
  set([0, u'work'])]

That I made from :

[u'meal',u'personal', u'sleep', u'transport', u'work']
['0:08:35.882945', 0, 0, 0, 0]

With this command:

nob =  [{m,n} for m,n in zip(cats,tot3)]

How can I turn this into a django-tables2 table?

I tried this :

# tables.py
class Small_table (tables.Table):
    category = tables.Column(verbose_name="category")
    class Meta:
        attrs = {"class": "paleblue"}

# views.py
nt = Small_table(nob)
RequestConfig(request).configure(nt)

But the table has one column of dashes rather than my data, what should I change?


Solution

  • this is what i ended up doing:

    in my tables.py:

    class Small_table (tables.Table):
        name = tables.Column(verbose_name="category",order_by="name")
        tot  = tables.Column(orderable=False)
        class Meta:
            attrs = {"class": "paleblue"}
    

    in my view

    from .tables import Small_table
    from django_tables2   import RequestConfig
    nob =  [{"name":m,"tot":n} for m,n in zip(cats,tot3)]
    nt = Small_table(nob)
    RequestConfig(request).configure(nt)
    return render(request, "job/job_home.html", { "small":nt })
    

    and in the template:

    {% load render_table from django_tables2 %}
     <link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue
    {% render_table small %}