Steve Piercy
2016-06-16 676e45f202061e86781fc6474931955caa73222d
Merge pull request #2651 from stevepiercy/1.6-branch

Quick Tour - fix static assets bug and clean up (backport from master)
4 files modified
41 ■■■■ changed files
docs/quick_tour.rst 35 ●●●● patch | view | raw | blame | history
docs/quick_tour/json/hello_world.jinja2 2 ●●● patch | view | raw | blame | history
docs/quick_tour/static_assets/hello_world.jinja2 2 ●●● patch | view | raw | blame | history
docs/quick_tour/static_assets/hello_world_static.jinja2 2 ●●● patch | view | raw | blame | history
docs/quick_tour.rst
@@ -67,6 +67,7 @@
.. 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:
@@ -117,11 +118,14 @@
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
@@ -154,6 +158,7 @@
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
@@ -164,6 +169,7 @@
responses:
.. literalinclude:: quick_tour/views/views.py
    :language: python
    :linenos:
We have four views, each leading to the other. If you start at
@@ -209,6 +215,7 @@
this route declaration, for example:
.. literalinclude:: quick_tour/routing/app.py
    :language: python
    :linenos:
    :lines: 6
    :lineno-start: 6
@@ -217,6 +224,7 @@
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
@@ -255,6 +263,7 @@
configuration in ``app.py``:
.. literalinclude:: quick_tour/templating/app.py
    :language: python
    :linenos:
    :lines: 6-8
    :lineno-start: 6
@@ -263,6 +272,7 @@
Now lets change our ``views.py`` file:
.. literalinclude:: quick_tour/templating/views.py
    :language: python
    :linenos:
    :emphasize-lines: 4,6
@@ -299,6 +309,7 @@
configuration:
.. literalinclude:: quick_tour/jinja2/app.py
    :language: python
    :linenos:
    :lines: 6-8
    :lineno-start: 6
@@ -307,6 +318,7 @@
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
@@ -334,9 +346,10 @@
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
@@ -354,7 +367,7 @@
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
@@ -366,16 +379,16 @@
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>`,
@@ -391,6 +404,7 @@
supports this with a JSON renderer:
.. literalinclude:: quick_tour/json/views.py
    :language: python
    :linenos:
    :lines: 9-
    :lineno-start: 9
@@ -403,6 +417,7 @@
respond to a request for ``hello.json``.
.. literalinclude:: quick_tour/json/app.py
    :language: python
    :linenos:
    :lines: 6-8
    :lineno-start: 6
@@ -432,6 +447,7 @@
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
@@ -692,6 +708,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
@@ -746,7 +763,9 @@
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
docs/quick_tour/json/hello_world.jinja2
@@ -2,7 +2,7 @@
<html lang="en">
<head>
    <title>Hello World</title>
    <link rel="stylesheet" href="{{ request.static_url('static/app.css') }}"/>
    <link rel="stylesheet" href="{{ request.static_url('__main__:static/app.css') }}"/>
</head>
<body>
<h1>Hello {{ name }}!</h1>
docs/quick_tour/static_assets/hello_world.jinja2
@@ -2,7 +2,7 @@
<html lang="en">
<head>
    <title>Hello World</title>
    <link rel="stylesheet" href="/static/app.css"/>
    <link rel="stylesheet" href="{{ request.static_url('__main__:static/app.css') }}"/>
</head>
<body>
<h1>Hello {{ name }}!</h1>
docs/quick_tour/static_assets/hello_world_static.jinja2
@@ -2,7 +2,7 @@
<html lang="en">
<head>
    <title>Hello World</title>
    <link rel="stylesheet" href="{{ request.static_url('static/app.css') }}"/>
    <link rel="stylesheet" href="/static/app.css"/>
</head>
<body>
<h1>Hello {{ name }}!</h1>