Default, or fallback, view in traversal?

39 views
Skip to first unread message

Isaac Jurado

unread,
Jul 17, 2012, 9:53:49 AM7/17/12
to pylons-discuss
Hello,

I would like to know if there is a way to add a fallback view name
when
a context is found but none of the defined view names match. For
example, from the following example application:

# coding: utf8
from pyramid.config import Configurator

class Foo(dict):
pass

def make_root(request):
return {'foo': Foo()}

def foo(request):
return request.subpath

def bar(request):
return {"whoami": "bar", "subpath": request.subpath}

def start(global_config, **settings):
config = Configurator(settings=settings)
config.set_root_factory(make_root)
config.add_view(foo, context=Foo, renderer="json")
config.add_view(bar, name="bar", context=Foo, renderer="json")
return config.make_wsgi_app()

Traversal finds /foo and /foo/bar fine. However, would it be possible
to configure a view (with context=Foo) to be used when the view_name
is
not found?

Thanks in advance.

Message has been deleted

Isaac Jurado

unread,
Jul 18, 2012, 8:23:59 AM7/18/12
to pylons-...@googlegroups.com
On Tue, Jul 17, 2012 at 8:53 PM, askel <dumm...@mail.ru> wrote:
> Hi Isaac,
>
> I'm not a Pyramid expert but I'd play with mapper parameter of
> add_view or view_config for that.

Thanks, but I found what I was looking for:

from pyramid.view import notfound_view_config

@notfound_view_config(containment=Foo, renderer='json')
def fallback(request):
return {'fallback': True, 'view': request.view_name}

Considering that the traversal tree is location aware (with the
__parent__ and __name__ thing), it works as a charm.

Cheers.

--
Isaac Jurado
Internet Busines Solutions eConcept

"Everyone knows that debugging is twice as hard as writing a program in
the first place. So if you're as clever as you can be when you write it,
how will you ever debug it?"

Brian W. Kernighan
Reply all
Reply to author
Forward
0 new messages