Thomas
Thomas

Reputation: 83

Jquery Ajax call with django

I'm a bit stuck with the following situation:

I'm using an ajax call to submit a form, i use the form values to do some 'query' in django. When this happened and i got the correct queryset it delivers me an HttP response in the form of a csv. I setted up the headers and all stuff needed to get the 'save file as' window... unfortunately, this doesn't happen because the ajax call puts the response inside a div.

This is the code:

if (vsc=='True'){
    ajax_loader();
 // add pdf, csv $.ajax (todo)
        $.ajax({ 
  type: "POST",
        url: "./test",
            dataType: 'text',
  success: test,
  data: {host: ho, log: lo, severity: se, datetimestart: dts, datetimeend: dte, enabled: ena, csv: vsc, pdf: fdp}

       });

So inside this ajax function a call the url ./test. Inside that url i do some object filtering and i make a http response object and send it back. On success i should like to get to 'save file as', but at this moment i have no clue how to do this. I succeeded in showing the response in a div on my html page ./test but the 'save file as' never shows up.

My django view:

def export_csv(request,queryset)
# do some filtering on queryset and create a csv
response = HttpResponse(''.join(csv_export), mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=customreportexport.csv'
return response

This is my view, everything should be normal, i think it's really that ajax call... on 'success' i have to do something so it popups as 'save file as', only i don't know how. I Already tried some iframe aswell but than i have to give the 'source' as an url and that's not possible as the ajax call does the post of the parameters etc... any help on this? Previously i knew there was in some language something called 'IsUpload=true', too bad it isn't in jquery ajax....

Hope to hear from you guys

Upvotes: 0

Views: 1424

Answers (1)

jturnbull
jturnbull

Reputation: 720

It sounds like this shouldn't be an Ajax request - if you just POST that form then the user will go to the csv url and be prompted to download the file.

Upvotes: 3

Related Questions