我正在使用Django tables2
。我页面中有n
行,我想export only the rows which the user selects
。但是我找不到一种使它起作用的方法。官方文档无济于事。
当我选择要导出的行时,然后单击“导出”。 The entire table is exported.
tables.py
class TrackerTable(tables.Table):
export_formats = ['csv', 'xls', 'xlsx', 'json']
id = tables.Column()
id = tables.LinkColumn('posts:tracker_view',args=[A('pk')], empty_values=())
editable = tables.LinkColumn('posts:modify',args=[A('pk')] , empty_values=(), verbose_name='')
selection = tables.CheckBoxColumn(accessor=A('pk')t ,empty_values=(), footer='', attrs = { "th__input":{"onclick": "toggle(this)"}}, orderable=False)
def render_editable(self):
#return 'Edit'
return mark_safe('<center><p style="color:blue;">✍</p></center>')
class Meta:
model = models.Post
template_name = 'django_tables2/semantic.html'
sequence = ('selection','editable','id','...')
attrs = {'tbody': {'id': 'myTable'}}
views.py
table = TrackerTable(Post.objects.all(), order_by="-id")
RequestConfig(request).configure(table)
export_format = request.GET.get('_export', None)
if TableExport.is_valid_format(export_format):
exporter = TableExport(export_format, table, exclude_columns=('editable','selection'))
return exporter.response('table.{}'.format(export_format))
return render(request, 'posts/tracker_list.html', {'table': table})```