Pyramid and .ini configuration

前端 未结 1 660
忘掉有多难
忘掉有多难 2020-12-29 08:04

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

[app:main]
use = egg:MyProject
pyram         


        
相关标签:
1条回答
  • 2020-12-29 08:40

    Sure you can.

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

    For example, in your .ini:

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

    In your __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()
    

    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.

    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.

    0 讨论(0)
提交回复
热议问题