Skip to content Skip to sidebar Skip to footer

Web2py Db Is Not Defined

I'm trying to run a script at command line that uses the models with the following command: c:\web2py>python web2py.py -M -N -S automate -R applications/automate/modules/eventse

Solution 1:

edit2: I have db = DAL('sqlite://storage.sqlite') in my db.py so it should get loaded

Assuming db.py is in the /models folder, the db object created there will be available in later executed model files as well as in the controller and view, but it will not be available within modules that you import. Instead, you will have to pass the db object to a function or class in the module. Another option is to add the db object to the current thread local object, which can then be imported and accessed within the module:

In /models/db.py:

from gluon importcurrentdb= DAL('sqlite://storage.sqlite')
current.db = db

In /modules/eventserver.py:

from gluon import current
def somefunction():
    db = current.db
    [do something with db]

Note, if you do define the db object in the module, don't define it at the top level -- define it in a function or class.

For more details, see the book section on modules and current.

Post a Comment for "Web2py Db Is Not Defined"