Search code examples
djangotabulardjango-tables2

Django tables not updating column data


I'm trying to pull data from mongodb and display it as a table in display.html page.

My data from table looks something like this:

[{u'Subjects': u'[Sub1, Sub2, Sub3, Sub4]', u'Student Name': u'A'}, {u'Subjects': u'[Sub2, Sub12, Sub7, Sub9]', u'Student Name': u'B'},{u'Subjects': u'[Sub1, Sub2, Sub14]', u'Student Name': u'AC'},

Below is my code:

views.py

def data_list():
    dis_data = db_data.find()
    table = DataTable(list(dis_data))
    RequestConfig(request).configure(table)
    return render(request, 'app/dispaly.html', {'table': table})

My tables.py

import django_tables2 as tables

class DataTable(tables.Table):
       Names = tables.Column(verbose_name = "Student Name")
       Subjects = tables.Column()

display.html

<html>
<body>
{% load render_table from django_tables2 %}

<div>
{% render_table table %}

</div>
</body>
</html>

But when displaying its only displaying subjects but not Student Name. Where is it going wrong ?


Solution

  • You need to define the accessor in your Table Class's Name column:

    class DataTable(tables.Table):
           Names = tables.Column(verbose_name = "Student Name", accessor="Student Name")  # <-- Here
           Subjects = tables.Column()