3

だから私が欲しいのは、次のものを備えた1つのピラミッドアプリを持つことです:

  • ユーザーが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 に答える