如何使用config.add\u设置({'金字塔。包括':…})从包含的callab

2024-06-08 00:28:29 发布

您现在位置:Python中文网/ 问答频道 /正文

当我从cookie cutter创建一个默认的金字塔应用程序时,它生成了一个INI文件,其部分如下:

[app:myapp]
use = egg:myproject#myapp
pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes = pyramid_debugtoolbar

现在我尝试在python代码中添加这些相同的设置,使用__init__.py中的Configurator对象,我发现下面的工作方式似乎是相同的:

config.include('pyramid_debugtoolbar')
config.add_settings({
    'pyramid.reload_templates'      : 'true',
    'pyramid.debug_authorization'   : 'false',
    'pyramid.debug_notfound'        : 'false',
    'pyramid.debug_routematch'      : 'false',
    'pyramid.default_locale_name'   : 'en',
    'pyramid.includes'              : 'pyramid_debugtoolbar',
    })

但是,在python中应用这些设置时,第一行config.include('pyramid_debugtoolbar')是必需的,否则就不起作用。然而,在INI版本中,设置pyramid.includes = pyramid_debugtoolbar就足够了。你知道吗

进一步挖掘后

在我的代码中往上看,我发现设置确实是这样工作的。。。你知道吗

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application."""
    settings.update({'pyramid.includes':'pyramid_debugtoolbar'}) # SETTING HERE WORKS!
    with Configurator(settings=settings) as config:
        config.include(common_config)
        config.include('.routes')
        config.scan()

    return config.make_wsgi_app()

但不是这样。。。你知道吗

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application."""
    with Configurator(settings=settings) as config:
        config.add_settings({'pyramid.includes':'pyramid_debugtoolbar'}) # NO EFFECT!
        config.include(common_config)
        config.include('.routes')
        config.scan()

    return config.make_wsgi_app()

documentation for pyramid.config中,我发现了一个我怀疑的警告:

A configuration callable should be a callable that accepts a single argument named config, which will be an instance of a Configurator. However, be warned that it will not be the same configurator instance on which you call this method. The code which runs as a result of calling the callable should invoke methods on the configurator passed to it which add configuration state. The return value of a callable will be ignored.

为了猜测解决方案,我尝试用config.commit()config.begin()/config.end()的各种组合来包装我的config.add_settings(...),但这些组合都不起作用。你知道吗

我的问题:

如何使用config.add_settings(...)设置pyramid.includes?我想在多个金字塔应用程序所包含的common_config()可调用中实现这一点。你知道吗


Tags: debugaddpyramidconfigfalseappwhichsettings

热门问题