我在web2py模块中使用了一个函数,它应该引发一个HTTP异常。对于大多数功能(例如T
)我做
from gluon import current
def f(x):
return current.T(x)
但我做不到raise current.HTTP(...)
:我得到了
<type 'exceptions.AttributeError'> 'thread._local' object has no attribute 'HTTP'
那么有没有办法在web2py模块中使用HTTP()?
答案 0 :(得分:1)
最好的选择可能只是在模块中导入HTTP
:
from gluon.http import HTTP
def f(x):
raise HTTP(200, 'Hello')
或者,您可以将HTTP
对象显式添加到current
对象。在模型文件或相关控制器中:
current.HTTP = HTTP
然后,您就可以在导入current.HTTP
的任何模块中访问current
。
最后,整个web2py全局环境可通过current.globalenv
字典获取,因此在导入current
的任何模块中,您都可以执行以下操作:
raise current.globalenv['HTTP'](200, 'Hello')
web2py只会将request
,response
,session
,cache
和T
对象直接添加到current
,所以如果您需要要从全局环境访问任何其他对象,您必须显式添加它们或使用current.globalenv
。