auth在模块中不可用

时间:2013-12-31 11:44:44

标签: python web2py

我有一个web2py应用程序,我编写了各种模块,其中包含业务逻辑和数据库相关的东西。在我尝试访问auth.settings.table_user_name的其中一个文件中,但它不起作用,抛出错误为global name 'auth' is not defined。如果我在控制器中写相同的行,它的工作原理。但我想在模块文件中访问它。请建议我该怎么做。

2 个答案:

答案 0 :(得分:3)

在模型文件中,您可以在其中定义auth

from gluon import current

auth = Auth(db)
current.auth = auth

然后在一个模块中:

from gluon import current

def some_function():
    auth = current.auth
    ....

有关详细信息,请参阅http://web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules

答案 1 :(得分:0)

我遇到了非常相似的错误(“未定义名称'auth'”)。不得不在views.py的顶部添加from django.contrib import auth,并且有效。

相关问题