Try this:
Sorry for the wrong reply .The tutorial says that:
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=unruly.csv'
writer = csv.writer(response)
First create a response and then write the content