0

我在 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() 吗?

4

1 回答 1

1

最好的选择可能只是HTTP在模块中导入:

from gluon.http import HTTP

def f(x):
    raise HTTP(200, 'Hello')

或者,您可以将对象显式添加HTTPcurrent对象。在模型文件或相关控制器中:

current.HTTP = HTTP

然后您将能够访问current.HTTP您导入的任何模块current

最后,整个 web2py 全局环境都可以通过current.globalenv字典获得,因此在您 import 的任何模块中current,您都可以执行以下操作:

raise current.globalenv['HTTP'](200, 'Hello')

web2py 仅将requestresponsesessioncacheT对象直接添加到current,因此如果您想从全局环境访问任何其他对象,您必须显式添加它们或使用current.globalenv.

于 2018-04-20T13:46:56.890 回答