Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Defining a fallback view in traversal
  2. from pyramid.config import Configurator
  3.  
  4. class Foo(dict):
  5.     pass
  6.  
  7. def make_root(request):
  8.     return {'foo': Foo()}
  9.  
  10. def foo(request):
  11.     return request.subpath
  12.  
  13. def bar(request):
  14.     return {"whoami": "bar", "subpath": request.subpath}
  15.  
  16. def start(global_config, **settings):
  17.     config = Configurator(settings=settings)
  18.     config.set_root_factory(make_root)
  19.     config.add_view(foo, context=Foo, renderer="json")
  20.     config.add_view(bar, name="bar", context=Foo, renderer="json")
  21.     return config.make_wsgi_app()
  22.        
  23. config.add_view(any_other, name="*default*", context=Foo, ...)
  24.        
  25. @notfound_view_config(containment=Foo)
  26. def notfound(request):
  27.     return HTTPNotFound('no views for Foo detected for view %s' % request.view_name)
  28.        
  29. config.add_view(any_other, name="", context=Foo, ...)
  30.        
  31. from pyramid.view import notfound_view_config
  32.  
  33. @notfound_view_config()
  34. def notfound(request):
  35.     return Response('Not Found, dude', status='404 Not Found')
  36.  
  37. def main(globals, **settings):
  38.    config = Configurator()
  39.    config.scan()