Search code examples
djangoexportexport-to-exceldjango-tables2

django tables2 export to xlsx: How to set exported filename using FBV?


Django==1.11.7
django-tables2==1.10.0
tablib==0.11.2

Using function based views, how to set the exported filename?

The tables 2 documentation gives a solution for class based views, but is not clear on function based views: http://django-tables2.readthedocs.io/en/latest/pages/export.html?highlight=function%20based%20views

It mentions an "export_name", but where does this need to be set?

Trying this:

def export_payment_list(request):
    _table = PaymentTable4Export(Payment.objects.all().order_by('-payment_date'))
    RequestConfig(request).configure(_table)

    exporter = TableExport('xlsx', _table)
    return exporter.response('table.{}'.format('xlsx'), filename='my_test')

... results in an error:

Exception Type: TypeError
Exception Value:    
response() got multiple values for argument 'filename'

Thank you for your help


Solution

  • It looks like the first argument of response() is the filename.

    If you want the filename to be my_test.xlsx, try:

    return exporter.response(filename='my_test.xlsx')