I have a problem that I can't find nowhere a solution that match what I need.
I have this AJAX call when I click on a button:
$.ajax({
type: 'POST',
url: '{% url "tests" %}',
traditional: true,
data : {'mydata': list,"excel": "" },
success: function (data, textStatus) {
//Test
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("some error " + String(errorThrown) + String(textStatus) + String(XMLHttpRequest.responseText));
}
});
And in the views.py:
if request.method == "POST" and request.is_ajax():
if 'excel' in request.POST:
data = request.POST.getlist('mydata')
if data:
tests = Test.objects.filter(pk__in=data)
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=Test.xlsx'
xlsx_data = WriteToExcelTests(tests)
response.write(xlsx_data)
return response
This works fine if I do not use AJAX (because I have another case when I don't use AJAX) and the file is downloaded in the browser but in this case I can't download the file, it does not give any error but it does not download the file.
How can I force this to download the file?