我正在尝试从mongodb中提取数据并将其显示为display.html页中的表格。
我从表中获得的数据如下所示:
[{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'},
下面是我的代码:
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})
我的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>
但是,当显示其仅显示主题而不显示学生姓名时。哪里出问题了?
答案 0 :(得分:1)
您需要在表类的Name
列中定义accessor:
class DataTable(tables.Table):
Names = tables.Column(verbose_name = "Student Name", accessor="Student Name") # <-- Here
Subjects = tables.Column()