2012-01-03 3 views
0

@view_config level 수준에서 추가 매개 변수를 전달하는 방법이 있습니까?@view_config 데코레이터의 추가 매개 변수 전달

내가하는 방법에 대해 여러 @view_config 장식을 사용하지만 매개 변수를 전달 하나가 사용 된 표시하기 위해 싶습니다

:

@view_config(renderer="shape.mak", route_name='circle_route') 
@view_config(renderer="shape.mak", route_name='triangle route') 
def new_model(self, fparams=None, ppath=None): 
    #here, I'd like to capture a variable that tells me whether the cirlce or triangle route was used to access this method 

내가 내 경로를 통합 할 수 있습니다 실현 (예 : 모양 경로가) 및 추가 모양 유형에 대한 추가 매개 변수이지만 내 옵션을 알고 싶습니다.

답변

1

보기 구성은 선언적입니다. 이는 뷰가 렌더링되는 방식을 알지 못함을 의미하며 매번 동일한 사전 형식을 반환하고 선택한 렌더러는 조건부에 따라 처리합니다. 즉, custom_predicate에서 원하는 모든 작업을 수행 할 수 있기 때문에 원하는 경우이 패턴을 깰 수 있습니다.

def set_shape_predicate(shape): 
    def predicate(context, request): 
     request.shape = shape 
     return True 
    return predicate 

@view_config(renderer="shape.mak", route_name='circle_route', custom_predicates=[set_shape_predicate('circle')]) 
@view_config(renderer="shape.mak", route_name='triangle route', custom_predicates=[set_shape_predicate('triangle')]) 
def new_model(self): 
    # request.shape will be 'circle' or 'triangle' 

또한 단지 request.matched_route.name을 확인하고 URL을 파견 시나리오에서 경로 이름 ('circle_route' 또는 'triangle_route')의 각각에 비교 할 수있다.