global name 'response' is not defined

3,744 views
Skip to first unread message

Oliver Christen

unread,
Jun 29, 2010, 4:28:18 AM6/29/10
to pylons-discuss
hello

Im using pylons 0.9.7 in a project, but when I try to use the
theoretically global object "response", I get the following error
message in logs:

Error - <type 'exceptions.NameError'>: global name 'response' is not
defined

the error happens at this line in my code:

response.headers['content-type'] = 'application/x-json'

do I have to explicitly import some module or something ?

thanks in advance
Oliver

cd34

unread,
Jun 29, 2010, 11:46:09 AM6/29/10
to pylons-discuss
At the top of your controller, what are you importing from pylons?

from pylons import request, response, session, tmpl_context, config

If response isn't there, or, you have it imported as something else,
you just need to make sure that matches.

Wyatt Baldwin

unread,
Jun 29, 2010, 12:50:09 PM6/29/10
to pylons-discuss
Yes:

from pylons import response

Unlike, for example, PHP or JavaScript, there's not much that's
globally global (so to speak) in Python, except built-ins like `len`.
In JavaScript, if you define a var at the window level, that var is
available everywhere, which is great source of confusion and errors.
In Python, there's no analogous super-global namespace--you always
have to import the symbols you need, and this a good thing IMO.

With regard to `pylons.response` and friends, these are global in the
sense that there's a single object that you import and use in all your
application code, but they're not global in the sense of existing in a
top-level global scope.

Also, a tiny shortcut: you can do `response.content_type` instead of
`response.headers['content-type']` (and there are similar attributes
for other HTTP headers).
Reply all
Reply to author
Forward
0 new messages