Your code already includes from gluon import *
, which means you have imported the thread local current
object. That object includes the response
object for the current request, so you should refer to current.response
rather than just response
.
Note, this is not necessary in model, controller, and view files because those files are executed in a global environment that already includes the response
object (along with much of the rest of the web2py API).
For more details, see http://web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules.