I want to let the user submit a form with some filter criteria and respond with a csv file. This is my code:
template:
<form action="{% url 'filter_tracks' %}" method="get" id="filter-form">
<various filter params>
...
<a class="md-btn" href="javascript:void(0)" onclick="$('#filter-form').submit();" id="csv_btn">Export</a>
</form>
views.py
filename = "TRACKS_EXP{x}_{z}.csv".format(x=int(time.time()), z=lang)
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="{x}"'.format(x=filename)
response.write(u'\ufeff'.encode('utf8'))
writer = csv.writer(response, delimiter=',', dialect='excel', quotechar = "\"", lineterminator='\r\n',)
for track in track_list:
writer.writerow([...])
return response
Although it works,
I get the data inside my HTML page and not in a downloadable file.
What am I doing wrong?