金字塔和.ini配置 [英] Pyramid and .ini configuration

查看:95
本文介绍了金字塔和.ini配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个Pyramid应用程序都有一个关联的.ini文件,其中包含其设置。例如,默认值可能类似于:

Each Pyramid application has an associated .ini file that contains its settings. For example, a default might look like:

[app:main]
use = egg:MyProject
pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
...

我想知道是否可以在其中添加您自己的配置值,并在运行时读取它们(主要是从可调用的视图中读取)。例如,我可能想拥有

I am wondering if it is possible to add your own configuration values in there, and read them at run-time (mostly from a view callable). For instance, I might want to have

[app:main]
blog.title = "Custom blog name"
blog.comments_enabled = true
...

还是更好?

推荐答案

确定可以。

在您的入口函数( main(global_config,** settings) __ init __。py 中在大多数情况下),您可以在 settings 变量中访问您的配置。

In your entry point function (main(global_config, **settings) in __init__.py in most cases), your config is accessible in the settings variable.

例如,在您的 .ini

[app:main]
blog.title = "Custom blog name"
blog.comments_enabled = true

在您的 __ init __。py :

def main(global_config, **settings):
    config = Configurator(settings=settings)
    blog_title = settings['blog.title']
    # you can also access you settings via config
    comments_enabled = config.registry.settings['blog.comments_enabled']
    return config.make_wsgi_app()

根据最新的金字塔文档,您可以通过 request.registry.settings 在视图功能中访问设置。而且,据我所知,它将通过 event.request.registry.settings 进入活动订阅者。

According to the latest Pyramid docs, you can access the settings in a view function via request.registry.settings. Also, as far as I know, it will be in event subscribers via event.request.registry.settings.

关于您有关使用其他文件的问题,我敢肯定,将所有配置放入常规init文件中是一种好习惯,就像您一样使用点分符号。

Regarding your question about using another file, I'm pretty sure it's good practice to put all your config in the regular init file, using dotted notation like you did.

这篇关于金字塔和.ini配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆