(这是我在StackOverFlow上的第一篇文章,它是一个很棒的资源和社区< 3)
我开发了一个网站,并使用django_tables2
来显示我的结果。
我的问题是我的桌面模型(非常简单)......
#tables.py
class myTable(TableReport, ColumnShiftTable):
column_default_show = ['select'];
class Meta:
attrs = {'class': 'table table-bordered table-striped table-condensed'};
我希望动态显示一组列...
#views.py
def visualiserGeneByHost(request):
#Get the list of Host
ReferenceList = exportdata();
#Get a hash (in keys the name of the genes and in value an object with values and host name.
geneByHostHash = exportGeneByHost(ReferenceList);
data = [];
for key in geneByHostHash:
list=geneByIsolatHash.get(key);
for element in list:
data.append({'gene':key, element.Reference:element.pc_identity})
table = ResistanceTable(data=data)
return render(request,'visualisation/page_bootstrap_GeneByHost.html' , {'table':table} )
geneByHostHash
看起来像这样:
{'rrsH_[Escherichia coli]': [< X instance at 0x7f4abdcef440>, <X instance at 0x7f4abdcef488>, <X instance at 0x7f4abdcef8c0>], 'fusA1_[Klebsiella pneumoniae]': [<X instance at 0x7f4abdcef878>], ......}
...但我不知道如何添加列,因为列列表不稳定。
#page_bootstrap_GeneByHost.html
<!-- Table loader -->
<div class="row">
<form action="{% url 'visualiserGeneByHost' %}" method="post">
{% csrf_token %}
{% load django_tables2 %}
{% render_table table %}
</form>
</div>
我会得到像该表一样的结果 - &gt; enter image description here 我已经拥有了所有数据,例如Genes的名称,值和每个主机的名称。
我希望我能理解。
修改
我在extra_columns
中查看了django_tables2
arg,但我__init__
与TableReport
冲突了ColumnShiftTable
。 :/
修改
我这样解决了这个问题:
class ResistanceTable(tables.Table):
souche = tables.Column();
column_default_show = ['souche']
def __init__(self, data, newscolumns, *args, **kwargs):
if newscolumns:
for col in newscolumns:
self.base_columns[col] = tables.Column(verbose_name=col)
super(ResistanceTable, self).__init__( data, newscolumns, *args, **kwargs)
class Meta:
attrs = {'class': 'table table-bordered table-striped table-condensed'}
#init a new table with data and news columns !!!!!
table = ResistanceTable(data=data,newscolumns=geneList)