如何使用表中的值初始化 TurboGears 2 中的全局变量

发布于 2024-08-06 05:38:56 字数 831 浏览 8 评论 0原文

我需要一个可以从模板调用的全局变量。

我在 lib 目录中编辑了 app_globals.py 来声明 PATH_TO_IMAGES ,如下所示

class Globals(object):
    """Container for objects available throughout the life of the application.

    One instance of Globals is created during application initialization and
    is available during requests via the 'app_globals' variable.

    """

    PATH_TO_IMAGES = ""

    def __init__(self):
        """Do nothing, by default."""
        pass

现在我可以从任何模板调用图像路径,如下所示

<img src="${g.PATH_TO_IMAGES}/${p.image}" />

图像路径存储在应用程序数据库上的设置表内,但我无法从全局声明中初始化它,我收到这个错误:

sqlalchemy.exc.UnboundExecutionError: 无法找到配置的绑定 映射器 映射器|设置|设置, SQL 表达式或此会话

我的猜测是数据库绑定发生在 Globals 初始化之后。所以我的问题是,哪个是在 TurboGears 2 中初始化全局变量的最佳位置以及最佳实践。

I need a global variable that I can call from the templates.

I edited app_globals.py in lib directory to declare PATH_TO_IMAGES like this

class Globals(object):
    """Container for objects available throughout the life of the application.

    One instance of Globals is created during application initialization and
    is available during requests via the 'app_globals' variable.

    """

    PATH_TO_IMAGES = ""

    def __init__(self):
        """Do nothing, by default."""
        pass

Now I can call from any template the image path like this

<img src="${g.PATH_TO_IMAGES}/${p.image}" />

The image path is stored inside a settings table on the app's database, but I can't initialize it from Globals declaration, i get this error:

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

My guess is that database binding happens after Globals is initialized. So my questions is, which is the best place to initialize a global variable in TurboGears 2 and which is the best practice to that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫雪独思 2024-08-13 05:38:56

只需使用缓存的属性即可:

class Globals(object):
    """Container for objects available throughout the life of the application.

    One instance of Globals is created during application initialization and
    is available during requests via the 'app_globals' variable.

    """

    @property
    def PATH_TO_IMAGES(self):
        try:
            return self._path_to_images
        except AttributeError:
            self._path_to_images = db_session.query(XXX)  # Fill in your query here
            return self._path_to_images

PS:您的问题确实是一个通用的 Python 问题。我建议您在发布其他类似问题之前先阅读官方 Python 文档。

Just use a cached property:

class Globals(object):
    """Container for objects available throughout the life of the application.

    One instance of Globals is created during application initialization and
    is available during requests via the 'app_globals' variable.

    """

    @property
    def PATH_TO_IMAGES(self):
        try:
            return self._path_to_images
        except AttributeError:
            self._path_to_images = db_session.query(XXX)  # Fill in your query here
            return self._path_to_images

PS : your question is a generic Python question really. I suggest you read the official Python docs before posting other similar questions.

帅哥哥的热头脑 2024-08-13 05:38:56

您可能需要创建自己的数据库连接才能从数据库获取此数据。

用 SQLAlchemy 术语来说,您需要创建自己的引擎、会话等。只需确保完成后进行清理即可。

我可能会使用 on_startup 将其放入配置中,然后如果您仍然需要的话,稍后将其粘贴到 Globals 对象中。

You probably need to create your own database connection to get this data from the database.

In SQLAlchemy terms, you'll want to create your own engine, session, etc. Just make sure to clean up after you're done.

I would probably do this in app_cfg.py using on_startup to get it into the config, and then stick it in the Globals object later on if you still need to.

笑叹一世浮沉 2024-08-13 05:38:56

模型初始化后,您可以将 PATH_TO_IMAGES 设置为其确定值。较早位于 model/init.py 中声明的“init_model”函数的末尾。

You may set PATH_TO_IMAGES to it's definite value once the models are initialized. The sooner being at the end of the 'init_model' function declared in model/init.py.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文