Thanks for the suggestion. Do you mean something like this?

@view_config(route_name='route1', renderer='/path/route1.template')
def route1(request):
    raise WhateverException(template='/path/route1.template')

@view_config(context=WhateverException)
def whatever_handler(exception, request):
    template = exception.template

    return render_to_response(template, {'a': 'b'}, request=request)


This would require making sure I'm passing the correct template to 
WhateverException and keeping them in sync.

I see there's a request.matched_route.name which would return 'route1' and 
request.matched_route.pattern which would return '/url/to/route1'. I could 
probably use request.matched_route.name to retrieve the original renderer, 
but I'm unaware of what API methods I'd need to call to go from 'route1' to 
'/path/route1.template'. Any ideas of how I can manage this? I've looked 
through the Pyramid code for ViewsConfiguratorMixin's add_view() to get 
some clues but it's quite complex and I can't make sense of it. Anyone have 
an idea?


I think having separate exceptions could get unwieldy if the only purpose 
of different exception types is to direct to the correct template. 

On Tuesday, December 30, 2014 1:05:46 AM UTC+1, Randall Leeds wrote:
>
> I don't know if there's a way to get the original view, but you could 
> always subclass the exception so that you can register separate exception 
> views for each case.
>  
> On Mon, Dec 29, 2014, 16:02 pyramidX <[email protected] <javascript:>> 
> wrote:
>
>> In an exception handler view like this, I'm rendering the 
>> "hardcoded.template" template in all cases.
>>
>> @view_config(context=WhateverException, renderer=
>> '/path/hardcoded.template')
>> def whatever_handler(exception, request):
>>     return {'a': 'b'}
>>
>> But WhateverException can be raised originally in various views, each 
>> using their own template.
>>
>> @view_config(route_name='route1', renderer='/path/route1.template')
>> def route1(request):
>>     raise WhateverException()
>>
>> @view_config(route_name='route2', renderer='/path/route2.template')
>> def route2(request):
>>     raise WhateverException()
>>
>> How can I make the exception handler view choose the template 
>> dynamically, selecting the template of the view where WhateverException was 
>> initially raised? (Instead of hardcoding it in the @view_config decorator 
>> as I'm doing now.)
>>
>> I think I should probably use something like this (remove renderer 
>> argument in @view_config and use render_to_response). Is this right?
>>
>> @view_config(context=WhateverException)
>> def whatever_handler(exception, request):
>>     template = # How to get the template e.g. /path/route1.template?
>>
>>     return render_to_response(template, {'a': 'b'}, request=request)
>>
>>
>> What's the correct way to get the original template, like 
>> /path/route1.template, of the view that raised WhateverException?
>>
>>  -- 
>> You received this message because you are subscribed to the Google Groups 
>> "pylons-discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/pylons-discuss.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Reply via email to