| | |
| | | |
| | | .. literalinclude:: quick_tour/hello_world/app.py |
| | | :linenos: |
| | | :language: python |
| | | |
| | | This simple example is easy to run. Save this as ``app.py`` and run it: |
| | | |
| | |
| | | Let's see some features of requests and responses in action: |
| | | |
| | | .. literalinclude:: quick_tour/requests/app.py |
| | | :language: python |
| | | :pyobject: hello_world |
| | | |
| | | In this Pyramid view, we get the URL being visited from ``request.url``. Also |
| | | if you visited http://localhost:6543/?name=alice in a browser, the name is |
| | | included in the body of the response:: |
| | | included in the body of the response: |
| | | |
| | | .. code-block:: text |
| | | |
| | | URL http://localhost:6543/?name=alice with name: alice |
| | | |
| | |
| | | First our revised ``app.py``: |
| | | |
| | | .. literalinclude:: quick_tour/views/app.py |
| | | :language: python |
| | | :linenos: |
| | | |
| | | We added some more routes, but we also removed the view code. Our views and |
| | |
| | | responses: |
| | | |
| | | .. literalinclude:: quick_tour/views/views.py |
| | | :language: python |
| | | :linenos: |
| | | |
| | | We have four views, each leading to the other. If you start at |
| | |
| | | this route declaration, for example: |
| | | |
| | | .. literalinclude:: quick_tour/routing/app.py |
| | | :language: python |
| | | :linenos: |
| | | :lines: 6 |
| | | :lineno-start: 6 |
| | |
| | | and ``smith`` to ``last``. We can then use this data in our view: |
| | | |
| | | .. literalinclude:: quick_tour/routing/views.py |
| | | :language: python |
| | | :linenos: |
| | | :lines: 5-8 |
| | | :lineno-start: 5 |
| | |
| | | configuration in ``app.py``: |
| | | |
| | | .. literalinclude:: quick_tour/templating/app.py |
| | | :language: python |
| | | :linenos: |
| | | :lines: 6-8 |
| | | :lineno-start: 6 |
| | |
| | | Now lets change our ``views.py`` file: |
| | | |
| | | .. literalinclude:: quick_tour/templating/views.py |
| | | :language: python |
| | | :linenos: |
| | | :emphasize-lines: 4,6 |
| | | |
| | |
| | | configuration: |
| | | |
| | | .. literalinclude:: quick_tour/jinja2/app.py |
| | | :language: python |
| | | :linenos: |
| | | :lines: 6-8 |
| | | :lineno-start: 6 |
| | |
| | | The only change in our view is to point the renderer at the ``.jinja2`` file: |
| | | |
| | | .. literalinclude:: quick_tour/jinja2/views.py |
| | | :language: python |
| | | :linenos: |
| | | :lines: 4-6 |
| | | :lineno-start: 4 |
| | |
| | | Of course the Web is more than just markup. You need static assets: CSS, JS, |
| | | and images. Let's point our web app at a directory from which Pyramid will |
| | | serve some static assets. First let's make another call to the |
| | | :term:`configurator`: |
| | | :term:`configurator` in ``app.py``: |
| | | |
| | | .. literalinclude:: quick_tour/static_assets/app.py |
| | | :language: python |
| | | :linenos: |
| | | :lines: 6-8 |
| | | :lineno-start: 6 |
| | |
| | | All we need to do now is point to it in the ``<head>`` of our Jinja2 template, |
| | | ``hello_world.jinja2``: |
| | | |
| | | .. literalinclude:: quick_tour/static_assets/hello_world.jinja2 |
| | | .. literalinclude:: quick_tour/static_assets/hello_world_static.jinja2 |
| | | :language: jinja |
| | | :linenos: |
| | | :lines: 4-6 |
| | |
| | | changes the arrangement on disk? Pyramid provides a helper to allow flexibility |
| | | on URL generation: |
| | | |
| | | .. literalinclude:: quick_tour/static_assets/hello_world_static.jinja2 |
| | | .. literalinclude:: quick_tour/static_assets/hello_world.jinja2 |
| | | :language: jinja |
| | | :linenos: |
| | | :lines: 4-6 |
| | | :lineno-start: 4 |
| | | :emphasize-lines: 2 |
| | | |
| | | By using ``request.static_url`` to generate the full URL to the static |
| | | assets, you both ensure you stay in sync with the configuration and |
| | | gain refactoring flexibility later. |
| | | By using ``request.static_url`` to generate the full URL to the static assets, |
| | | you ensure that you stay in sync with the configuration and gain refactoring |
| | | flexibility later. |
| | | |
| | | .. seealso:: See also: |
| | | :ref:`Quick Tutorial Static Assets <qtut_static_assets>`, |
| | |
| | | supports this with a JSON renderer: |
| | | |
| | | .. literalinclude:: quick_tour/json/views.py |
| | | :language: python |
| | | :linenos: |
| | | :lines: 9- |
| | | :lineno-start: 9 |
| | |
| | | respond to a request for ``hello.json``. |
| | | |
| | | .. literalinclude:: quick_tour/json/app.py |
| | | :language: python |
| | | :linenos: |
| | | :lines: 6-8 |
| | | :lineno-start: 6 |
| | |
| | | save a change, or press the delete button in our ``views.py``: |
| | | |
| | | .. literalinclude:: quick_tour/view_classes/views.py |
| | | :language: python |
| | | :linenos: |
| | | :lines: 7- |
| | | :lineno-start: 7 |
| | |
| | | Our unit test passed. What did our test look like? |
| | | |
| | | .. literalinclude:: quick_tour/package/hello_world/tests.py |
| | | :language: python |
| | | :linenos: |
| | | |
| | | Pyramid supplies helpers for test writing, which we use in the test setup and |
| | |
| | | |
| | | Our application, a package named ``hello_world``, is set up as a logger and |
| | | configured to log messages at a ``DEBUG`` or higher level. When you visit |
| | | http://localhost:6543, your console will now show:: |
| | | http://localhost:6543, your console will now show: |
| | | |
| | | .. code-block:: text |
| | | |
| | | 2016-01-18 13:55:55,040 DEBUG [hello_world.views:10][waitress] Some Message |
| | | |