3

所以我想要的是有一个金字塔应用程序具有以下内容:

  • 如果用户访问control.blah.com/我希望他们获得页面 A
  • 如果用户去www.blah.com/我希望他们得到页面 B

我尝试了什么:

# Pregen for "Control" Subdomain
def pregen(request, elements, kw):
    kw['_app_url'] = 'http://control.blah.com'
    return elements, kw

# Custom predicate for "Control" Subdomain
def req_sub(info, request):
    return request.host.startswith('control')

def main(global_config, **settings):
    """
    This function returns a Pyramid WSGI application.
    """

    engine = engine_from_config(settings, 'sqlalchemy.')
    Session.configure(bind=engine)

    config = Configurator(settings=settings)
    config.include('pyramid_jinja2')
    config.add_jinja2_extension('jinja2.ext.do')
    config.include('pyramid_tm')
    config.add_static_view('static', 'static', cache_max_age=3600)

    # Control Subdomain
    config.add_route('control_index', '/', custom_predicates=(req_sub, ),
            pregenerator=pregen)

    # Main Subdomain
    config.add_route('index', '/')

    config.scan()

    app = config.make_wsgi_app()

    return app

所以现在,去control.blah.com/导致control_index视图被调用,但是当我去时,www.blah.com/我得到一个 404 Not Found。

同样,如果我config.add_route('index', '/')在子域行之前移动,那么我会遇到相反的问题。

是否有可能得到我想要的,或者路线需要有不同的模式(AKA。不能有 2 条路线与/模式)

4

1 回答 1

0

您对自定义谓词的使用看起来是正确的,但您可能对与相同模式冲突的路线是正确的。这是我会尝试的:

class DomainOverrideMiddleware(object):

    def __init__(self, app):
        self.application = app

    def __call__(self, environ, start_response):
        if "HTTP_HOST" in environ and "control.blah.com" in environ['HTTP_HOST']:
            environ['PATH_INFO'] = "/control" + environ['PATH_INFO']
        return self.application(environ, start_response)



def main(global_config, **settings):
    """
    This function returns a Pyramid WSGI application.
    """

    engine = engine_from_config(settings, 'sqlalchemy.')
    Session.configure(bind=engine)

    config = Configurator(settings=settings)
    config.include('pyramid_jinja2')
    config.add_jinja2_extension('jinja2.ext.do')
    config.include('pyramid_tm')
    config.add_static_view('static', 'static', cache_max_age=3600)

    # Control Subdomain
    #Change this subdomain to handle the modified path
    config.add_route('control_index', '/control/', custom_predicates=(req_sub, ),
            pregenerator=pregen)

    # Main Subdomain
    config.add_route('index', '/')

    config.scan()

    #Change this to call the override
    app = DomainOverrideMiddleware(config.make_wsgi_app())

因此,这会在应用程序内部使用“/control”前缀修改路径以处理不同的路由(避免冲突),但用户的路径应该保持不变。未经测试,但理论上可行。

于 2013-12-06T03:50:22.230 回答