Reputation: 53
I am trying to use the example of Django tables2 to export xls and csv. this is the part of my html:
{% for format in table.export_formats %}
<a href="{% export_url format %}">
download <code>.{{ format }}</code>
</a>
{% endfor %}
My web page is class view based with filter, I can see my web url with query string attached with '?_export=xls', which is expected. However, when I clicked, no response, out output file created? not sure if anything else to make it work?
My work is almost the same as this example, but no output created when clicked. Any ideas? thanks a lot.
I am using django tables 2 and trying to export my table following the official documentation
Upvotes: 4
Views: 4513
Reputation: 116
In your template:
{% load querystring from django_tables2 %}
<a href="{% querystring '_export'='csv' %}">Download CSV</a>
<a href="{% querystring '_export'='xlsx' %}">Download XLSX</a>
In your view:
from django_tables2.export.export import TableExport
export_format = request.GET.get('_export', None)
if TableExport.is_valid_format(export_format):
table = [[your table object]]
exporter = TableExport(export_format, table)
return exporter.response('File_Name.{}'.format(export_format))
The second line is to check if the url was clicked and the _export flag is included in the request. print request.GET
if you want to see what's included in the request and you should see '_export' as a key with the format as the value:
{'_export':'csv'}
Upvotes: 7