2017-01-23 10 views
1

일부 로깅과 같이 모든/모든보기가 호출되기 바로 전에 일부 코드 만 실행하고 싶습니다.피라미드에서는 뷰가 호출되기 직전에 발생하는 "후크"가 있습니까?

피라미드에서이를 수행 할 수있는 방법이 있습니까?

+1

내가보기 Derivers 살펴 보았다,하지만 당신은 자동으로 메시지 때마다 로깅의 내 목적을 패배보기별로보기에 그들을 활성화해야 보기를 수정하지 않고도보기가 호출됩니다. –

+0

사실이 아닙니다. 뷰 도우미는 모든 뷰를 래핑합니다. 각 뷰를 개별적으로 래핑하여 뷰 단위 옵션을 허용하고 특정 뷰를 래핑하는 것을 피할 수 있습니다.하지만 이는 본문에 달려 있습니다. –

답변

2

모두 트윈 [1] deriver보기 [2] 문서 타이밍 코드 예제가 있습니다. 측정하고자하는 대상과 측정 할 때 사용할 정보에 따라 달라집니다. 예를 들어 트윈은 어떤 뷰가 실행되었는지 알지 못하고 URL 만 알 수 있습니다. 그러나 그것은 더 많은 파이프 라인을 포함합니다.

[1] http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html#creating-a-tween

[2] http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html#custom-view-derivers

2

일부 이벤트에 가입하면 그렇게 할 수 있습니다.

http://docs.pylonsproject.org/projects/pyramid/en/latest/api/events.html

당신이 원하는 하나는 아마도 ContextFound입니다.

당신은 장식 (http://docs.pylonsproject.org/projects/pyramid/en/latest/api/events.html#pyramid.events.subscriber)와 중 하나를 구독 할 수 있습니다 : add_subscriber와 명령 적

from pyramid.events import ContextFound 
from pyramid.events import subscriber 

@subscriber(ContextFound) 
def do_something(event): 
    print(event) 
    print(event.request) 

또는 (http://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html#pyramid.config.Configurator.add_subscriber) :

from pyramid.events import ContextFound 

def main(global_config, **settings): 
    # rest of your config here 
    config.add_subscriber(do_something, ContextFound) 

def do_something(event): 
    print(event) 
    print(event.request)