convert stray references to colon routing syntax to squiggly syntax
| | |
| | | ``http://example.com/members/Chris``, where it's assumed that each |
| | | item "below" ``members`` in the URL represents a single member in some |
| | | system. You just match everything "below" ``members`` to a particular |
| | | :term:`view callable`, e.g. ``/members/:memberid``. |
| | | :term:`view callable`, e.g. ``/members/{memberid}``. |
| | | |
| | | However, URL dispatch is not very convenient if you'd like your URLs |
| | | to represent an arbitrary hierarchy. For example, if you need to |
| | |
| | | |
| | | <route |
| | | name="myroute" |
| | | pattern="/prefix/:one/:two" |
| | | pattern="/prefix/{one}/{two}" |
| | | view=".views.myview" |
| | | /> |
| | | |
| | |
| | | |
| | | .. code-block:: python |
| | | |
| | | config.add_handler('hello', '/hello/:action', handler=Hello) |
| | | config.add_handler('hello', '/hello/{action}', handler=Hello) |
| | | |
| | | This example will result in a route being added for the pattern |
| | | ``/hello/:action``, each method of the ``Hello`` class will then be examined |
| | | to register the views. The value of ``:action`` in the route pattern will be |
| | | ``/hello/{action}``, each method of the ``Hello`` class will then be examined |
| | | to register the views. The value of ``{action}`` in the route pattern will be |
| | | used to determine which view should be called, and each view in the class will |
| | | be setup with a view predicate that requires a specific ``action`` name. |
| | | |
| | |
| | | |
| | | .. code-block:: python |
| | | |
| | | config.add_handler('hello', '/hello/:action', |
| | | config.add_handler('hello', '/hello/{action}', |
| | | handler='mypackage.handlers:MyHandler') |
| | | |
| | | In larger applications, it is advised to use a :term:`resource specification` |
| | |
| | | return {} |
| | | |
| | | # in the config |
| | | config.add_handler('hello', '/hello/:action', handler=Hello) |
| | | config.add_handler('hello', '/hello/{action}', handler=Hello) |
| | | |
| | | With this configuration, the url ``/hello/home`` will find a view configuration |
| | | that results in calling the ``show_template`` method, then rendering the |
| | |
| | | |
| | | # config is an instance of pyramid.configuration.Configurator |
| | | |
| | | config.add_route('foobar', ':foo/:bar', view='myproject.views.foobar') |
| | | config.add_route('bazbuz', ':baz/:buz', view='myproject.views.bazbuz') |
| | | config.add_route('foobar', '{foo}/{bar}', view='myproject.views.foobar') |
| | | config.add_route('bazbuz', '{baz}/{buz}', view='myproject.views.bazbuz') |
| | | |
| | | Each :term:`route` typically corresponds to a single view callable, |
| | | and when that route is matched during a request, the view callable |
| | |
| | | .. code-block:: python |
| | | :linenos: |
| | | |
| | | config.add_route('home', ':foo/:bar/*traverse') |
| | | config.add_route('home', '{foo}/{bar}/*traverse') |
| | | |
| | | A ``*traverse`` token at the end of the pattern in a route's |
| | | configuration implies a "remainder" *capture* value. When it is used, |
| | |
| | | .. code-block:: python |
| | | :linenos: |
| | | |
| | | config.add_route('home', ':foo/:bar/*traverse', |
| | | config.add_route('home', '{foo}/{bar}/*traverse', |
| | | factory='mypackage.routes.root_factory') |
| | | |
| | | The ``factory`` above points at the function we've defined. It |
| | |
| | | |
| | | When the route configuration named ``home`` above is matched during a |
| | | request, the matchdict generated will be based on its pattern: |
| | | ``:foo/:bar/*traverse``. The "capture value" implied by the |
| | | ``{foo}/{bar}/*traverse``. The "capture value" implied by the |
| | | ``*traverse`` element in the pattern will be used to traverse the |
| | | graph in order to find a context, starting from the root object |
| | | returned from the root factory. In the above example, the |
| | | :term:`root` object found will be the instance named ``root`` in |
| | | ``routes.py``. |
| | | |
| | | If the URL that matched a route with the pattern ``:foo/:bar/*traverse``, |
| | | If the URL that matched a route with the pattern ``{foo}/{bar}/*traverse``, |
| | | is ``http://example.com/one/two/a/b/c``, the traversal path used |
| | | against the root object will be ``a/b/c``. As a result, |
| | | :app:`Pyramid` will attempt to traverse through the edges ``a``, |
| | |
| | | .. code-block:: python |
| | | :linenos: |
| | | |
| | | config.add_route('home', ':foo/:bar/*traverse', |
| | | config.add_route('home', '{foo}/{bar}/*traverse', |
| | | factory='mypackage.routes.root_factory') |
| | | config.add_view('mypackage.views.myview', route_name='home') |
| | | |
| | |
| | | .. code-block:: python |
| | | :linenos: |
| | | |
| | | config.add_route('home', ':foo/:bar/*traverse', |
| | | config.add_route('home', '{foo}/{bar}/*traverse', |
| | | factory='mypackage.routes.root_factory') |
| | | config.add_view('mypackage.views.myview', name='home') |
| | | config.add_view('mypackage.views.another_view', name='another', |
| | |
| | | .. code-block:: python |
| | | :linenos: |
| | | |
| | | config.add_route('abc', '/articles/:article/edit', |
| | | traverse='/articles/:article') |
| | | config.add_route('abc', '/articles/{article}/edit', |
| | | traverse='/articles/{article}') |
| | | |
| | | The syntax of the ``traverse`` argument is the same as it is for |
| | | ``pattern``. |
| | | |
| | | If, as above, the ``pattern`` provided is ``articles/:article/edit``, |
| | | and the ``traverse`` argument provided is ``/:article``, when a |
| | | If, as above, the ``pattern`` provided is ``articles/{article}/edit``, |
| | | and the ``traverse`` argument provided is ``/{article}``, when a |
| | | request comes in that causes the route to match in such a way that the |
| | | ``article`` match value is ``1`` (when the request URI is |
| | | ``/articles/1/edit``), the traversal path will be generated as ``/1``. |
| | |
| | | .. code-block:: python |
| | | :linenos: |
| | | |
| | | config.add_route('home', ':foo/:bar/*traverse', |
| | | config.add_route('home', '{foo}/{bar}/*traverse', |
| | | view='myproject.views.home') |
| | | config.add_view('myproject.views.another', route_name='home') |
| | | |
| | |
| | | .. code-block:: python |
| | | :linenos: |
| | | |
| | | config.add_route('home', ':foo/:bar/*traverse', |
| | | config.add_route('home', '{foo}/{bar}/*traverse', |
| | | view='myproject.views.home') |
| | | |
| | | Can also be spelled like so: |
| | |
| | | .. code-block:: python |
| | | :linenos: |
| | | |
| | | config.add_route('home', ':foo/:bar/*traverse') |
| | | config.add_route('home', '{foo}/{bar}/*traverse') |
| | | config.add_view('myproject.views.home', route_name='home') |
| | | |
| | | The two spellings are logically equivalent. In fact, the former is |
| | |
| | | match has an attribute named ``matchdict`` that contains the elements placed |
| | | into the URL by the ``pattern`` of a ``route`` statement. For instance, if a |
| | | call to :meth:`pyramid.configuration.Configurator.add_route` in |
| | | ``__init__.py`` had the pattern ``:one/:two``, and the URL at |
| | | ``__init__.py`` had the pattern ``{one}/{two}``, and the URL at |
| | | ``http://example.com/foo/bar`` was invoked, matching this pattern, the |
| | | matchdict dictionary attached to the request passed to the view would have a |
| | | ``one`` key with the value ``foo`` and a ``two`` key with the value ``bar``. |
| | |
| | | to the view named ``view_wiki`` in our ``views.py`` file with the name |
| | | ``view_wiki``. This is the :term:`default view` for the wiki. |
| | | |
| | | #. Add a declaration which maps the pattern ``/:pagename`` to the view named |
| | | #. Add a declaration which maps the pattern ``/{pagename}`` to the view named |
| | | ``view_page`` in our ``views.py`` file with the view name ``view_page``. |
| | | This is the regular view for a page. |
| | | |
| | | #. Add a declaration which maps the pattern |
| | | ``/add_page/:pagename`` to the view named ``add_page`` in our |
| | | ``/add_page/{pagename}`` to the view named ``add_page`` in our |
| | | ``views.py`` file with the name ``add_page``. This is the add view |
| | | for a new page. |
| | | |
| | | #. Add a declaration which maps the pattern ``/:pagename/edit_page`` to the |
| | | #. Add a declaration which maps the pattern ``/{pagename}/edit_page`` to the |
| | | view named ``edit_page`` in our ``views.py`` file with the name |
| | | ``edit_page``. This is the edit view for a page. |
| | | |
| | |
| | | view_renderer='tutorial:templates/login.pt') |
| | | config.add_route('logout', '/logout', |
| | | view='tutorial.login.logout') |
| | | config.add_route('view_page', '/:pagename', |
| | | config.add_route('view_page', '/{pagename}', |
| | | view='tutorial.views.view_page', |
| | | view_renderer='tutorial:templates/view.pt') |
| | | config.add_route('add_page', '/add_page/:pagename', |
| | | config.add_route('add_page', '/add_page/{pagename}', |
| | | view='tutorial.views.add_page', |
| | | view_renderer='tutorial:templates/edit.pt', |
| | | view_permission='edit') |
| | | config.add_route('edit_page', '/:pagename/edit_page', |
| | | config.add_route('edit_page', '/{pagename}/edit_page', |
| | | view='tutorial.views.edit_page', |
| | | view_renderer='tutorial:templates/edit.pt', |
| | | view_permission='edit') |
| | |
| | | return DBSession |
| | | |
| | | def _registerRoutes(config): |
| | | config.add_route('view_page', ':pagename') |
| | | config.add_route('edit_page', ':pagename/edit_page') |
| | | config.add_route('add_page', 'add_page/:pagename') |
| | | config.add_route('view_page', '{pagename}') |
| | | config.add_route('edit_page', '{pagename}/edit_page') |
| | | config.add_route('add_page', 'add_page/{pagename}') |
| | | |
| | | class ViewWikiTests(unittest.TestCase): |
| | | def setUp(self): |
| | |
| | | |
| | | def test_it(self): |
| | | from tutorial.views import view_wiki |
| | | self.config.add_route('view_page', ':pagename') |
| | | self.config.add_route('view_page', '{pagename}') |
| | | request = testing.DummyRequest() |
| | | response = view_wiki(request) |
| | | self.assertEqual(response.location, 'http://example.com/FrontPage') |
| | |
| | | config = Configurator(settings=settings) |
| | | config.add_static_view('static', 'tutorial:static') |
| | | config.add_route('home', '/', view='tutorial.views.view_wiki') |
| | | config.add_route('view_page', '/:pagename', |
| | | config.add_route('view_page', '/{pagename}', |
| | | view='tutorial.views.view_page', |
| | | view_renderer='tutorial:templates/view.pt') |
| | | config.add_route('add_page', '/add_page/:pagename', |
| | | config.add_route('add_page', '/add_page/{pagename}', |
| | | view='tutorial.views.add_page', |
| | | view_renderer='tutorial:templates/edit.pt') |
| | | config.add_route('edit_page', '/:pagename/edit_page', |
| | | config.add_route('edit_page', '/{pagename}/edit_page', |
| | | view='tutorial.views.edit_page', |
| | | view_renderer='tutorial:templates/edit.pt') |
| | | return config.make_wsgi_app() |
| | |
| | | return DBSession |
| | | |
| | | def _registerRoutes(config): |
| | | config.add_route('view_page', ':pagename') |
| | | config.add_route('edit_page', ':pagename/edit_page') |
| | | config.add_route('add_page', 'add_page/:pagename') |
| | | config.add_route('view_page', '{pagename}') |
| | | config.add_route('edit_page', '{pagename}/edit_page') |
| | | config.add_route('add_page', 'add_page/{pagename}') |
| | | |
| | | class ViewWikiTests(unittest.TestCase): |
| | | def setUp(self): |
| | |
| | | |
| | | def test_it(self): |
| | | from tutorial.views import view_wiki |
| | | self.config.add_route('view_page', ':pagename') |
| | | self.config.add_route('view_page', '{pagename}') |
| | | request = testing.DummyRequest() |
| | | response = view_wiki(request) |
| | | self.assertEqual(response.location, 'http://example.com/FrontPage') |
| | |
| | | ~~~~~~~~~~ |
| | | |
| | | ``pattern`` |
| | | The pattern of the route e.g. ``ideas/:idea``. This attribute is |
| | | The pattern of the route e.g. ``ideas/{idea}``. This attribute is |
| | | required. See :ref:`route_pattern_syntax` for information |
| | | about the syntax of route patterns. |
| | | |
| | |
| | | |
| | | The syntax of the ``traverse`` argument is the same as it is for |
| | | ``pattern``. For example, if the ``pattern`` provided to the |
| | | ``route`` directive is ``articles/:article/edit``, and the |
| | | ``route`` directive is ``articles/{article}/edit``, and the |
| | | ``traverse`` argument provided to the ``route`` directive is |
| | | ``/:article``, when a request comes in that causes the route to |
| | | ``/{article}``, when a request comes in that causes the route to |
| | | match in such a way that the ``article`` match value is '1' (when |
| | | the request URI is ``/articles/1/edit``), the traversal path will be |
| | | generated as ``/1``. This means that the root object's |
| | |
| | | |
| | | The syntax of the ``traverse`` argument is the same as it is |
| | | for ``pattern``. For example, if the ``pattern`` provided to |
| | | ``add_route`` is ``articles/:article/edit``, and the |
| | | ``add_route`` is ``articles/{article}/edit``, and the |
| | | ``traverse`` argument provided to ``add_route`` is |
| | | ``/:article``, when a request comes in that causes the route |
| | | ``/{article}``, when a request comes in that causes the route |
| | | to match in such a way that the ``article`` match value is |
| | | '1' (when the request URI is ``/articles/1/edit``), the |
| | | traversal path will be generated as ``/1``. This means that |
| | |
| | | |
| | | pattern |
| | | |
| | | The pattern of the route e.g. ``ideas/:idea``. This |
| | | The pattern of the route e.g. ``ideas/{idea}``. This |
| | | argument is required. See :ref:`route_path_pattern_syntax` |
| | | for information about the syntax of route patterns. If the |
| | | pattern doesn't match the current URL, route matching |
| | |
| | | session_factory = session_factory_from_settings(settings) |
| | | config.set_session_factory(session_factory) |
| | | config.add_static_view('static', '{{package}}:static/') |
| | | config.add_handler('action', '/:action', |
| | | config.add_handler('action', '/{action}', |
| | | '{{package}}.handlers.hello:HelloHandler') |
| | | config.add_handler('home', '/', '{{package}}.handlers.hello:HelloHandler', |
| | | action='index') |
| | |
| | | session_factory = session_factory_from_settings(settings) |
| | | config.set_session_factory(session_factory) |
| | | config.add_static_view('static', '{{package}}:static/') |
| | | config.add_handler('action', '/:action', '{{package}}.handlers:MyHandler') |
| | | config.add_handler('action', '/{action}', '{{package}}.handlers:MyHandler') |
| | | config.add_handler('home', '/', '{{package}}.handlers:MyHandler', |
| | | action='index') |
| | | config.add_subscriber('{{package}}.subscribers.add_renderer_globals', |
| | |
| | | session_factory = session_factory_from_settings(settings) |
| | | config.set_session_factory(session_factory) |
| | | config.add_static_view('static', '{{package}}:static/') |
| | | config.add_handler('main', '/:action', '{{package}}.handlers:MyHandler') |
| | | config.add_handler('main', '/{action}', '{{package}}.handlers:MyHandler') |
| | | config.add_handler('home', '/', '{{package}}.handlers:MyHandler', |
| | | action='index') |
| | | config.add_subscriber('{{package}}.subscribers.add_renderer_globals', |
| | |
| | | enough arguments, for example). |
| | | |
| | | For example, if you've defined a route named "foobar" with the path |
| | | ``:foo/:bar/*traverse``:: |
| | | ``:foo/{bar}/*traverse``:: |
| | | |
| | | route_url('foobar', request, foo='1') => <KeyError exception> |
| | | route_url('foobar', request, foo='1', bar='2') => <KeyError exception> |
| | |
| | | and anchor data are present in the returned string. |
| | | |
| | | For example, if you've defined a route named 'foobar' with the path |
| | | ``/:foo/:bar``, this call to ``route_path``:: |
| | | ``/{foo}/{bar}``, this call to ``route_path``:: |
| | | |
| | | route_path('foobar', request, foo='1', bar='2') |
| | | |