Possibly related topic, but from which I could not solve the problem: How to initialize global variables in TurboGears 2 with values from a table

The issue is that I'm trying to read some config parameters from the database when initializing some of the global variables in app_globals.py in Turbogears.

When attempting to access the database however, I receive an exception:

sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|Config|config_table, SQL expression or this Session

Just as the exception states this is probably due to the db not being bound yet (global parameters are set up first). From the other topic I was pointed in the direction of going to app_cfg.py and using "on_startup" to bind the database before initializing the global variables, but it's not working for me. Either I'm not doing it right or this is not the solution to the problem. For instance, I tried writing:

def on_startup():
    print "Foo bar!!!"

base_config.register_hook('startup', on_startup)

But the print statement is never executed (the crash occurs before).

Does anyone have any pointers on where to go from here?

Thanks in advance.

有帮助吗?

解决方案

You can use the after_config hook to call a setup method in your app_globals which gets to the database and retrieves the data it needs to set itself up.

after_config hooks is actually called after the full turbogears application got configured and so you will have both app_globals and the database built and configured. You can access app_globals from the hook using config['tg.app_globals'] or config['pylons.app_globals'] depending on which version of TG you are using.

Pay attention that the after_config hook has to return the turbogears application itself, so just return the parameter you get.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top