From b1b92284f496800a4dfd2cea72cb9be07ba8661c Mon Sep 17 00:00:00 2001 From: Paul Everitt Date: Fri, 13 Sep 2013 16:52:14 -0400 Subject: First cut at import of quick tutorial. --- docs/quick_tutorial/routing.rst | 119 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 docs/quick_tutorial/routing.rst (limited to 'docs/quick_tutorial/routing.rst') diff --git a/docs/quick_tutorial/routing.rst b/docs/quick_tutorial/routing.rst new file mode 100644 index 000000000..39597d996 --- /dev/null +++ b/docs/quick_tutorial/routing.rst @@ -0,0 +1,119 @@ +========================================== +11: Dispatching URLs To Views With Routing +========================================== + +Routing matches incoming URL patterns to view code. Pyramid's routing +has a number of useful features. + +Background +========== + +Writing web applications usually means sophisticated URL design. We +just saw some Pyramid machinery for requests and views. Let's look at +features that help in routing. + +Previously we saw the basics of routing URLs to views in Pyramid: + +- Your project's "setup" code registers a route name to be used when + matching part of the URL + +- Elsewhere, a view is configured to be called for that route name + +.. note:: + + Why do this twice? Other Python web frameworks let you create a + route and associate it with a view in one step. As + illustrated in :ref:`routes_need_ordering`, multiple routes might match the + same URL pattern. Rather than provide ways to help guess, Pyramid lets you + be explicit in ordering. Pyramid also gives facilities to avoid the + problem. It's relatively easy to build a system that uses implicit route + ordering with Pyramid too. See `The Groundhog series of screencasts + `_ if you're interested in + doing so. + +Objectives +========== + +- Define a route that extracts part of the URL into a Python dictionary + +- Use that dictionary data in a view + +Steps +===== + +#. First we copy the results of the ``view_classes`` step: + + .. code-block:: bash + + (env27)$ cd ..; cp -r view_classes routing; cd routing + (env27)$ python setup.py develop + +#. Our ``routing/tutorial/__init__.py`` needs a route with a replacement + pattern: + + .. literalinclude:: routing/tutorial/__init__.py + :linenos: + +#. We just need one view in ``routing/tutorial/views.py``: + + .. literalinclude:: routing/tutorial/views.py + :linenos: + +#. We just need one view in ``routing/tutorial/home.pt``: + + .. literalinclude:: routing/tutorial/home.pt + :language: html + :linenos: + +#. Update ``routing/tutorial/tests.py``: + + .. literalinclude:: routing/tutorial/tests.py + :linenos: + +#. Now run the tests: + + .. code-block:: bash + + (env27)$ nosetests tutorial + +#. Run your Pyramid application with: + + .. code-block:: bash + + (env27)$ pserve development.ini --reload + +#. Open ``http://localhost:6543/howdy/amy/smith`` in your browser. + +Analysis +======== + +In ``__init__.py`` we see an important change in our route declaration: + +.. code-block:: python + + config.add_route('hello', '/howdy/{first}/{last}') + +With this we tell the :term:`pyramid:configurator` that our URL has +a "replacement pattern". With this, URLs such as ``/howdy/amy/smith`` +will assign ``amy`` to ``first`` and ``smith`` to ``last``. We can then +use this data in our view: + +.. code-block:: python + + self.request.matchdict['first'] + self.request.matchdict['last'] + +``request.matchdict`` contains values from the URL that match the +"replacement patterns" (the curly braces) in the route declaration. +This information can then be used anywhere in Pyramid that has access +to the request. + +Extra Credit +============ + +#. What happens if you to go the URL + ``http://localhost:6543/howdy``? Is this the result that you + expected? + +.. seealso:: `Weird Stuff You Can Do With URL + Dispatch `_ -- cgit v1.2.3 From 4042c772c8043ac96a22db439a736fec9ea2aafa Mon Sep 17 00:00:00 2001 From: Paul Everitt Date: Fri, 13 Sep 2013 17:09:35 -0400 Subject: All the references re-wired. --- docs/quick_tutorial/routing.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docs/quick_tutorial/routing.rst') diff --git a/docs/quick_tutorial/routing.rst b/docs/quick_tutorial/routing.rst index 39597d996..33b1edb28 100644 --- a/docs/quick_tutorial/routing.rst +++ b/docs/quick_tutorial/routing.rst @@ -12,7 +12,7 @@ Writing web applications usually means sophisticated URL design. We just saw some Pyramid machinery for requests and views. Let's look at features that help in routing. -Previously we saw the basics of routing URLs to views in Pyramid: +Previously we saw the basics of routing URLs to views in - Your project's "setup" code registers a route name to be used when matching part of the URL @@ -93,7 +93,7 @@ In ``__init__.py`` we see an important change in our route declaration: config.add_route('hello', '/howdy/{first}/{last}') -With this we tell the :term:`pyramid:configurator` that our URL has +With this we tell the :term:`configurator` that our URL has a "replacement pattern". With this, URLs such as ``/howdy/amy/smith`` will assign ``amy`` to ``first`` and ``smith`` to ``last``. We can then use this data in our view: -- cgit v1.2.3 From 0a784868bdbc3a0eb226ed00e8d89cda9d181ec5 Mon Sep 17 00:00:00 2001 From: Paul Everitt Date: Fri, 13 Sep 2013 17:11:42 -0400 Subject: Fix naming of virtualenv prefix. --- docs/quick_tutorial/routing.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/quick_tutorial/routing.rst') diff --git a/docs/quick_tutorial/routing.rst b/docs/quick_tutorial/routing.rst index 33b1edb28..b1cb0d806 100644 --- a/docs/quick_tutorial/routing.rst +++ b/docs/quick_tutorial/routing.rst @@ -45,8 +45,8 @@ Steps .. code-block:: bash - (env27)$ cd ..; cp -r view_classes routing; cd routing - (env27)$ python setup.py develop + (env)$ cd ..; cp -r view_classes routing; cd routing + (env)$ python setup.py develop #. Our ``routing/tutorial/__init__.py`` needs a route with a replacement pattern: @@ -74,13 +74,13 @@ Steps .. code-block:: bash - (env27)$ nosetests tutorial + (env)$ nosetests tutorial #. Run your Pyramid application with: .. code-block:: bash - (env27)$ pserve development.ini --reload + (env)$ pserve development.ini --reload #. Open ``http://localhost:6543/howdy/amy/smith`` in your browser. -- cgit v1.2.3 From b0b28ede912c817a62a84b97c332e39eda02d166 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Mon, 16 Sep 2013 02:14:53 +0200 Subject: s/env/venv just for sake of consistency --- docs/quick_tutorial/routing.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/quick_tutorial/routing.rst') diff --git a/docs/quick_tutorial/routing.rst b/docs/quick_tutorial/routing.rst index b1cb0d806..95205e97a 100644 --- a/docs/quick_tutorial/routing.rst +++ b/docs/quick_tutorial/routing.rst @@ -45,8 +45,8 @@ Steps .. code-block:: bash - (env)$ cd ..; cp -r view_classes routing; cd routing - (env)$ python setup.py develop + (venv)$ cd ..; cp -r view_classes routing; cd routing + (venv)$ python setup.py develop #. Our ``routing/tutorial/__init__.py`` needs a route with a replacement pattern: @@ -74,13 +74,13 @@ Steps .. code-block:: bash - (env)$ nosetests tutorial + (venv)$ nosetests tutorial #. Run your Pyramid application with: .. code-block:: bash - (env)$ pserve development.ini --reload + (venv)$ pserve development.ini --reload #. Open ``http://localhost:6543/howdy/amy/smith`` in your browser. -- cgit v1.2.3 From d749bf4c987c4ab90bd5f89326e7d4059e4f47b3 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Mon, 16 Sep 2013 02:18:11 +0200 Subject: make example links clickable, for convenience --- docs/quick_tutorial/routing.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docs/quick_tutorial/routing.rst') diff --git a/docs/quick_tutorial/routing.rst b/docs/quick_tutorial/routing.rst index 95205e97a..86fc04570 100644 --- a/docs/quick_tutorial/routing.rst +++ b/docs/quick_tutorial/routing.rst @@ -82,7 +82,7 @@ Steps (venv)$ pserve development.ini --reload -#. Open ``http://localhost:6543/howdy/amy/smith`` in your browser. +#. Open http://localhost:6543/howdy/amy/smith in your browser. Analysis ======== @@ -112,7 +112,7 @@ Extra Credit ============ #. What happens if you to go the URL - ``http://localhost:6543/howdy``? Is this the result that you + http://localhost:6543/howdy? Is this the result that you expected? .. seealso:: `Weird Stuff You Can Do With URL -- cgit v1.2.3 From 187104fd81418beeb51592913041d9751bafe08d Mon Sep 17 00:00:00 2001 From: Paul Everitt Date: Wed, 25 Sep 2013 09:27:43 -0400 Subject: Quick Tutorial: Improve the setup instructions (adapted from Steve Piercy's work), particularly for Windows. Change all the steps to use $VENV/bin prefixes on commands (don't presume that they have done source env/bin/activate). --- docs/quick_tutorial/routing.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/quick_tutorial/routing.rst') diff --git a/docs/quick_tutorial/routing.rst b/docs/quick_tutorial/routing.rst index 86fc04570..bfae69615 100644 --- a/docs/quick_tutorial/routing.rst +++ b/docs/quick_tutorial/routing.rst @@ -45,8 +45,8 @@ Steps .. code-block:: bash - (venv)$ cd ..; cp -r view_classes routing; cd routing - (venv)$ python setup.py develop + $ cd ..; cp -r view_classes routing; cd routing + $ $VENV/bin/python setup.py develop #. Our ``routing/tutorial/__init__.py`` needs a route with a replacement pattern: @@ -74,13 +74,13 @@ Steps .. code-block:: bash - (venv)$ nosetests tutorial + $ $VENV/bin/nosetests tutorial #. Run your Pyramid application with: .. code-block:: bash - (venv)$ pserve development.ini --reload + $ $VENV/bin/pserve development.ini --reload #. Open http://localhost:6543/howdy/amy/smith in your browser. -- cgit v1.2.3 From b731b5fca253d9d95b3307490aa585e194676c01 Mon Sep 17 00:00:00 2001 From: Paul Everitt Date: Thu, 26 Sep 2013 17:41:44 -0400 Subject: Quick Tour: shorten the setup part and point to Quick Tutorial Requirements for more explanation. Cross link each Quick Tour section with its Quick Tutorial match. --- docs/quick_tutorial/routing.rst | 2 ++ 1 file changed, 2 insertions(+) (limited to 'docs/quick_tutorial/routing.rst') diff --git a/docs/quick_tutorial/routing.rst b/docs/quick_tutorial/routing.rst index bfae69615..54dff5c39 100644 --- a/docs/quick_tutorial/routing.rst +++ b/docs/quick_tutorial/routing.rst @@ -1,3 +1,5 @@ +.. _qtut_routing: + ========================================== 11: Dispatching URLs To Views With Routing ========================================== -- cgit v1.2.3