Steve Piercy
2017-10-22 4567204570eff25408278fd01919c1b048b9f7f1
commit | author | age
b731b5 1 .. _qtut_routing:
PE 2
b1b922 3 ==========================================
PE 4 11: Dispatching URLs To Views With Routing
5 ==========================================
6
72c219 7 Routing matches incoming URL patterns to view code. Pyramid's routing has a
SP 8 number of useful features.
9
b1b922 10
PE 11 Background
12 ==========
13
72c219 14 Writing web applications usually means sophisticated URL design. We just saw
SP 15 some Pyramid machinery for requests and views. Let's look at features that help
16 in routing.
b1b922 17
4caf31 18 Previously we saw the basics of routing URLs to views in Pyramid.
b1b922 19
72c219 20 - Your project's "setup" code registers a route name to be used when matching
SP 21   part of the URL
b1b922 22
72c219 23 - Elsewhere a view is configured to be called for that route name.
b1b922 24
PE 25 .. note::
26
6c36d7 27     Why do this twice? Other Python web frameworks let you create a route and
SP 28     associate it with a view in one step. As illustrated in
29     :ref:`routes_need_ordering`, multiple routes might match the same URL
30     pattern. Rather than provide ways to help guess, Pyramid lets you be
31     explicit in ordering. Pyramid also gives facilities to avoid the problem.
32     It's relatively easy to build a system that uses implicit route ordering
33     with Pyramid too. See `The Groundhog series of screencasts
34     <http://static.repoze.org/casts/videotags.html>`_ if you're interested in
b1b922 35     doing so.
PE 36
72c219 37
b1b922 38 Objectives
PE 39 ==========
40
72c219 41 - Define a route that extracts part of the URL into a Python dictionary.
b1b922 42
72c219 43 - Use that dictionary data in a view.
SP 44
b1b922 45
PE 46 Steps
47 =====
48
49 #. First we copy the results of the ``view_classes`` step:
50
51    .. code-block:: bash
52
187104 53     $ cd ..; cp -r view_classes routing; cd routing
e612f1 54     $ $VENV/bin/pip install -e .
b1b922 55
PE 56 #. Our ``routing/tutorial/__init__.py`` needs a route with a replacement
57    pattern:
58
59    .. literalinclude:: routing/tutorial/__init__.py
60     :linenos:
61
62 #. We just need one view in ``routing/tutorial/views.py``:
63
64    .. literalinclude:: routing/tutorial/views.py
65     :linenos:
66
67 #. We just need one view in ``routing/tutorial/home.pt``:
68
69    .. literalinclude:: routing/tutorial/home.pt
70     :language: html
71     :linenos:
72
73 #. Update ``routing/tutorial/tests.py``:
74
75    .. literalinclude:: routing/tutorial/tests.py
76     :linenos:
77
78 #. Now run the tests:
79
80    .. code-block:: bash
81
251ddd 82     $ $VENV/bin/py.test tutorial/tests.py -q
72c219 83     ..
SP 84     2 passed in 0.39 seconds
b1b922 85
PE 86 #. Run your Pyramid application with:
87
88    .. code-block:: bash
89
187104 90     $ $VENV/bin/pserve development.ini --reload
b1b922 91
d749bf 92 #. Open http://localhost:6543/howdy/amy/smith in your browser.
72c219 93
b1b922 94
PE 95 Analysis
96 ========
97
98 In ``__init__.py`` we see an important change in our route declaration:
99
100 .. code-block:: python
101
102     config.add_route('hello', '/howdy/{first}/{last}')
103
72c219 104 With this we tell the :term:`configurator` that our URL has a "replacement
SP 105 pattern". With this, URLs such as ``/howdy/amy/smith`` will assign ``amy`` to
106 ``first`` and ``smith`` to ``last``. We can then use this data in our view:
b1b922 107
PE 108 .. code-block:: python
109
110     self.request.matchdict['first']
111     self.request.matchdict['last']
112
72c219 113 ``request.matchdict`` contains values from the URL that match the "replacement
SP 114 patterns" (the curly braces) in the route declaration. This information can
115 then be used anywhere in Pyramid that has access to the request.
b1b922 116
72c219 117 Extra credit
b1b922 118 ============
PE 119
72c219 120 #. What happens if you to go the URL http://localhost:6543/howdy? Is this the
SP 121    result that you expected?
b1b922 122
72c219 123 .. seealso:: `Weird Stuff You Can Do With URL Dispatch
SP 124    <http://www.plope.com/weird_pyramid_urldispatch>`_