Search code examples
pythoncsvturbogears2

Serving a CSV file for download


I am working with TurboGears 2.2.2. and need to serve a CSV file so that the user can download it.

This is the code that I have so far:

response.content_type = 'text/csv'
response.headers['Content-Disposition'] = 'attachment;filename=%s.csv' % file_name

listWriter = csv.writer(open(title+'.csv', 'wb'),
                delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)

listWriter.writerow(['foo', 'bar'])

with open (title+'.csv', "r") as myfile:
    response.body=myfile.read().replace('\n', '')

return response

The CSV is created but I can't serve it to the user for download. I am always getting this error:

"TypeError: No object (name: response) has been registered for this thread"

Any help, please?


Solution

  • This got answered on ML: https://groups.google.com/d/msg/turbogears/T7Gly2TZv_s/-_JCMXNZqagJ

    Return response.body instead of response will solve the issue