evg*_*eny 15 python ini configuration-files pyramid
每个Pyramid应用程序都有一个包含其设置的关联.ini文件.例如,默认值可能如下所示:
[app:main]
use = egg:MyProject
pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
...
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以在那里添加自己的配置值,并在运行时读取它们(主要来自可调用的视图).例如,我可能想要
[app:main]
blog.title = "Custom blog name"
blog.comments_enabled = true
...
Run Code Online (Sandbox Code Playgroud)
或者更好的是有一个单独的.ini文件并在启动时解析它?
Ant*_*air 27
你当然可以.
在入口点函数中(main(global_config, **settings)在__init__.py大多数情况下),您的配置可在settings变量中访问.
例如,在你的.ini:
[app:main]
blog.title = "Custom blog name"
blog.comments_enabled = true
Run Code Online (Sandbox Code Playgroud)
在你的__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()
Run Code Online (Sandbox Code Playgroud)
根据最新的Pyramid文档,您可以通过视图功能访问设置request.registry.settings.此外,据我所知,它将在事件订阅者通过event.request.registry.settings.
关于你关于使用另一个文件的问题,我非常确定将所有配置放在常规init文件中是一种好习惯,使用点缀符号.