Reputation: 289
I'm trying to download/export some data into an Excel File. At the end of my view/function that does this I have this return:
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename = "report.xls"'
response = render_to_response("facturas.html",context_instance=RequestContext(request), content_type='application/vnd.ms-excel')
book.save(response)
return response
I'm stablishing that is an excel file, the name of the file and I'm saving the data and returning the response
I'm creating the book variable at the beggining of the function:
book = xlwt.Workbook(encoding='utf8')
sheet = book.add_sheet('report')
and then applying some style, etc
What is happening is that the exporting part of the function is downloading a "FILE" type not an Excel tipe of file.
If you need me to add the complete code of the view I'll add it.
Does anyone have any idea why is this happenning?.
Any advice will be really appreciatted. Thanks
EDIT
The data that i´m trying to export is inside of a table AND the table is inside of a Form (I have checkboxes for each row and the user can select which files to export so with this i create a queryset of the files that are selected)
The Form needs a COntext_instance that´s why i´m using render_to_response.
How can i change this in the code?
Thanks for all the answers.
Upvotes: 0
Views: 63
Reputation: 12333
In line 3 of:
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename = "report.xls"'
response = render_to_response("facturas.html",context_instance=RequestContext(request), content_type='application/vnd.ms-excel')
book.save(response)
return response
You return an HTTP file as a download and lose the previous settings.
Remove line 3.
Upvotes: 1
Reputation: 5362
Sergio is right. Right now you're overriding (line 3) the response you've fabricated on line 1 and 2. You do not need the render_to_response
. If you want to start a download AND return some HTML: that's impossible. A single HTTP request simply can't return multiple HTTP responses.
Upvotes: 1