From 9b9e5cfc2ca1e960de5ca70b78931c429a9f75d2 Mon Sep 17 00:00:00 2001 From: Paul Winkler Date: Mon, 30 Jan 2012 13:07:01 -0500 Subject: [PATCH] Trivial traversal-in-views example; hopefully this adds an easy way to quickly 'get' traversal --- CONTRIBUTORS.txt | 3 +- index.rst | 1 + traversal.rst | 4 +++ traversal_in_views.rst | 78 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 traversal_in_views.rst diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index ec758fe..6f80115 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -105,4 +105,5 @@ Blaise Laflamme, 12/27/2010 Casey Duncan, 12/27/2010 Michael Merickel, 3/30/2011 Charlie Choiniere, 4/23/2011 -Steve Piercy, 5/10/2011 \ No newline at end of file +Steve Piercy, 5/10/2011 +Paul Winkler, 1/30/2012 diff --git a/index.rst b/index.rst index 21990d2..877e19d 100644 --- a/index.rst +++ b/index.rst @@ -27,6 +27,7 @@ The Pyramid Cookbook presents topical, practical usages of :mod:`Pyramid`. deployment/index.rst porting testing + traversal_in_views traversal cmf/index.rst mac_install diff --git a/traversal.rst b/traversal.rst index f6e41a7..e79dda9 100644 --- a/traversal.rst +++ b/traversal.rst @@ -152,3 +152,7 @@ This is simpler and more readable because we are using URL dispatch to take care of the hardcoded URLs at the top of the tree, and we are using traversal only for the arbitrarily nested subdirectories. +See Also +~~~~~~~~ + +- :doc:`traversal_in_views` diff --git a/traversal_in_views.rst b/traversal_in_views.rst new file mode 100644 index 0000000..f69ed02 --- /dev/null +++ b/traversal_in_views.rst @@ -0,0 +1,78 @@ +Using Traversal in Pyramid Views +================================= + +A trivial example of how to use :term:`traversal` in your view code. + +You may remember that a Pyramid :term:`view` is called with a +:term:`context` argument: + +.. code-block:: python + + def my_view(context, request): + return render_view_to_response(context, request) + + +When using traversal, ``context`` will be the :term:`resource` object +that was found by traversal. Configuring which resources a view +responds to can be done easily via either the ``@view.config`` +decorator... + +.. code-block:: python + + + from models import MyResource + + @view_config(context=MyResource) + def my_view(context, request): + return render_view_to_response(context, request) + +or via ``config.add_view``: + +.. code-block:: python + + from models import MyResource + config = Configurator() + config.add_view('myapp.views.my_view', context=MyResource) + +Either way, any request that triggers traversal and traverses to a +``MyResource`` instance will result in calling this view with that +instance as the ``context`` argument. + +Optional: Using Interfaces +-------------------------- + +If your resource classes implement :term:`interfaces `, +you can configure your views by interface. This is one way to decouple +view code from a specific resource implementation:: + + + # models.py + from zope.interface import implements + from zope.interface import Interface + + class IMyResource(Interface): + pass + + class MyResource(object): + implements(IMyResource) + + # views.py + from models import IMyResource + + @view_config(context=IMyResource) + def my_view(context, request): + return render_view_to_response(context, request) + + +See Also +-------- + +- :doc:`traversal` + +- The "Virginia" sample application: https://github.com/Pylons/virginia/blob/master/virginia/views.py + +- ZODB and Traversal in Pyramid tutorial: http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki/index.html#bfg-wiki-tutorial + +- "Pyramid Traversal and Mongodb": http://kusut.web.id/2011/03/27/pyramid-traversal-and-mongodb/ + +- Resources which implement interfaces: http://readthedocs.org/docs/pyramid/en/latest/narr/resources.html#resources-which-implement-interfaces