Curious, there is something wrong happening in Pylons/Paste.

I suppose what you reported is the body of your controller, have you tried
changing it to return the response body instead of the response object
itself?

Like:

from tg import response

class RootController(TGController):
   @expose()
   def mymethod(self):
        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'])

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

        return content

PS: instead of writing on a file to read it back, you probably want to use
StringIO.getvalue()


On Mon, Mar 17, 2014 at 12:32 PM, Ricardo Molina <
[email protected]> wrote:

> Sorry, I forgot.
>
> This is the traceback:
>
> Exception happened during processing of request from ('127.0.0.1', 53855)
> Traceback (most recent call last):
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/httpserver.py",
> line 1068, in process_request_in_thread
>     self.finish_request(request, client_address)
>   File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
>     self.RequestHandlerClass(request, client_address, self)
>   File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
>     self.handle()
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/httpserver.py",
> line 442, in handle
>     BaseHTTPRequestHandler.handle(self)
>   File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
>     self.handle_one_request()
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/httpserver.py",
> line 437, in handle_one_request
>     self.wsgi_execute()
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/httpserver.py",
> line 287, in wsgi_execute
>     self.wsgi_start_response)
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/cascade.py",
> line 130, in __call__
>     return self.apps[-1](environ, start_response)
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/registry.py",
> line 403, in __call__
>     reg.cleanup()
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/registry.py",
> line 349, in cleanup
>     stacked._pop_object(obj)
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/registry.py",
> line 273, in _pop_object_restoration
>     self._pop_object_orig(obj)
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/registry.py",
> line 230, in _pop_object
>     if obj and popped is not obj:
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/registry.py",
> line 175, in __nonzero__
>     return bool(self._current_obj())
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/registry.py",
> line 259, in _current_obj_restoration
>     return self._current_obj_orig()
>   File
> "/home/ricardo/env/tg222/local/lib/python2.7/site-packages/Paste-1.7.5.1-py2.7.egg/paste/registry.py",
> line 197, in _current_obj
>     'thread' % self.____name__)
> TypeError: No object (name: response) has been registered for this thread
>
>
> El lunes, 17 de marzo de 2014 12:27:50 UTC+1, Alessandro Molina escribió:
>>
>> Where are you getting the error?  Please always provide the traceback
>> when reporting exceptions :D
>>
>>
>> On Mon, Mar 17, 2014 at 12:16 PM, Ricardo Molina <[email protected]
>> > wrote:
>>
>>> I am working with TurboGears 2.2.2. and need to 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?
>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "TurboGears" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>>
>>> Visit this group at http://groups.google.com/group/turbogears.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/turbogears.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.

Reply via email to