我的设置文件定义如下(config/settings.py):
HOST='localhost'
PORT='9001'
当我在视图中导入设置并按如下方式打印每个设置的值时,它会正确打印(views/user.py):
from ..config import settings
print settings.HOST
print settings.PORT
但是当我从 def 内部使用或打印相同的值时,它会给出错误(views/user.py):
from ..config import settings
print settings.HOST
print settings.PORT
@handle_error
def usersettings():
print settings.HOST
print settings.PORT
在上述文件中调用函数 def 时会报错,如下所示:
ERROR:root:'function' object has no attribute 'HOST'
Traceback (most recent call last):
File "/home/rahul/mywebapp/webapp/views/utils.py", line 36, in decorated_view
return_value = func(*args, **kwargs)
File "/home/rahul/mywebapp/webapp/views/utils.py", line 27, in decorated_view
return func(*args, **kwargs)
File "/home/rahul/mywebapp/webapp/views/user.py", line 344, in usersettings
print settings.HOST
AttributeError: 'function' object has no attribute 'HOST'
utils.py 具有名为 handle_error 的修饰视图
我的包结构如下:
mywebapp/
run.py
webapp/
__init__.py
views/
__init__.py
utils.py
user.py
config/
__init__.py
settings.py
仅供参考:这个用法一直工作到昨晚,突然出现上述错误,它开始表现得很奇怪。我在这里做错了什么?
编辑:
我的init .py 如下:
from .utils import *
from .user import *