金字塔:config.set_request_属性回调永远不会被调用

2024-09-30 08:32:34 发布

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

我试图用config.set_request_property设置一个回调,但是这个回调永远不会被调用。没有错误消息,它只是默默地失败了。为什么不起作用?我怎样才能找到问题所在?在

以下是我在__init__中使用的代码:

def callbackTest(request):
    print 'Callback worked!'
    return True

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    # pyramid_beaker add-on  
    session_factory = session_factory_from_settings(settings)
    set_cache_regions_from_settings(settings)

    config = Configurator(root_factory=MongoRootFactory(settings), session_factory=session_factory, settings=settings)

    config.add_static_view('gfx', 'gfx', cache_max_age=3600)
    config.add_static_view('fonts', 'fonts', cache_max_age=3600)
    config.add_static_view('css', 'css/compiled', cache_max_age=3600)
    config.add_static_view('js', 'js/compiled', cache_max_age=3600)



    print 'callbackTest callback should be set...'
    config.set_request_property(callbackTest, 'user', reify=True)

    #********************************************************
    # Authentication
    #********************************************************
    authn_policy = AuthTktAuthenticationPolicy(secret='asecret',
                                           callback=groupfinder)
    authz_policy = ACLAuthorizationPolicy()
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(authz_policy)


    #********************************************************
    # View setup
    #********************************************************

    config.add_route('IndexTest', '/')
    config.add_route('Home', '/h')



    config.scan()
    return config.make_wsgi_app()

有人能看到我的错误吗?在


Tags: viewaddconfigcacheagesettingsrequestsession
2条回答

我看到的唯一错误是你没有告诉我们你在哪里使用这个物业?只有在调用属性时才会调用它们。您是否有在视图中调用request.user的代码,并且由于该属性不存在而导致出现错误?如果您希望每个请求都自动调用函数,那么set_request_property正试图避免这种情况。在

来自pyramid.configAPI文档:

callable can either be a callable that accepts the request as its single positional parameter[...]

现在我不知道这是否会导致您的问题,但它需要接受一个工作请求(尽管我希望抛出异常,而不是无声的失败)。在

相关问题 更多 >

    热门问题