سؤال

I have a web2py application where I have written various modules which hold business logic and database related stuff. In one of the files I am trying to access auth.settings.table_user_name but it doesn't work and throws and error as global name 'auth' is not defined. If I write the same line in controller, it works. But I want it to be accessed in module file. Please suggest how do I do that.

هل كانت مفيدة؟

المحلول

In your model file, where you define auth:

from gluon import current

auth = Auth(db)
current.auth = auth

Then in a module:

from gluon import current

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

For more details, see http://web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules.

نصائح أخرى

I was getting a very similar error ("name 'auth' is not defined"). Had to add from django.contrib import auth at the top of views.py and it worked.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top