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/sessions.rst | 98 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 docs/quick_tutorial/sessions.rst (limited to 'docs/quick_tutorial/sessions.rst') diff --git a/docs/quick_tutorial/sessions.rst b/docs/quick_tutorial/sessions.rst new file mode 100644 index 000000000..5de5e2958 --- /dev/null +++ b/docs/quick_tutorial/sessions.rst @@ -0,0 +1,98 @@ +================================= +17: Transient Data Using Sessions +================================= + +Store and retrieve non-permanent data in Pyramid sessions. + +Background +========== + +When people use your web application, they frequently perform a task +that requires semi-permanent data to be saved. For example, a shopping +cart. This is called a :term:`session`. + +Pyramid has basic built-in support for sessions, with add-ons such as +*dogpile.cache* (or your own custom sessioning engine) that provide +richer session support. Let's take a look at the +:ref:`built-in sessioning support `. + +Objectives +========== + +- Make a session factory using a built-in, simple Pyramid sessioning + system + +- Change our code to use a session + +Steps +===== + +#. First we copy the results of the ``view_classes`` step: + + .. code-block:: bash + + (env27)$ cd ..; cp -r view_classes sessions; cd sessions + (env27)$ python setup.py develop + +#. Our ``sessions/tutorial/__init__.py`` needs a choice of session + factory to get registered with the :term:`configurator`: + + .. literalinclude:: sessions/tutorial/__init__.py + :linenos: + +#. Our views in ``sessions/tutorial/views.py`` can now use + ``request.session``: + + .. literalinclude:: sessions/tutorial/views.py + :linenos: + +#. The template at ``sessions/tutorial/home.pt`` can display the value: + + .. literalinclude:: sessions/tutorial/home.pt + :language: html + :linenos: + +#. Make sure the tests still pass: + + .. code-block:: bash + + (env27)$ nosetests tutorial + +#. Run your Pyramid application with: + + .. code-block:: bash + + (env27)$ pserve development.ini --reload + +#. Open ``http://localhost:6543/`` and ``http://localhost:6543/howdy`` + in your browser. As you reload and switch between those URLs, note + that the counter increases and is *not* specific to the URL. + +#. Restart the application and revisit the page. Note that counter + still increases from where it left off. + +Analysis +======== + +Pyramid's :term:`request` object now has a ``session`` attribute +that we can use in our view code. It acts like a dictionary. + +Since all the views are using the same counter, we made the counter a +Python property at the view class level. With this, each reload will +increase the counter displayed in our template. + +In web development, "flash messages" are notes for the user that need +to appear on a screen after a future web request. For example, +when you add an item using a form ``POST``, the site usually issues a +second HTTP Redirect web request to view the new item. You might want a +message to appear after that second web request saying "Your item was +added." You can't just return it in the web response for the POST, +as it will be tossed out during the second web requests. + +Flash messages are a technique where messages can be stored between +requests, using sessions, then removed when they finally get displayed. + +.. seealso:: + :ref:`pyramid:sessions_chapter`, + :ref:`pyramid:flash_messages`, and + :ref:`pyramid:session_module`. -- 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/sessions.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/quick_tutorial/sessions.rst') diff --git a/docs/quick_tutorial/sessions.rst b/docs/quick_tutorial/sessions.rst index 5de5e2958..b50d6d318 100644 --- a/docs/quick_tutorial/sessions.rst +++ b/docs/quick_tutorial/sessions.rst @@ -14,7 +14,7 @@ cart. This is called a :term:`session`. Pyramid has basic built-in support for sessions, with add-ons such as *dogpile.cache* (or your own custom sessioning engine) that provide richer session support. Let's take a look at the -:ref:`built-in sessioning support `. +:ref:`built-in sessioning support `. Objectives ========== @@ -93,6 +93,6 @@ Flash messages are a technique where messages can be stored between requests, using sessions, then removed when they finally get displayed. .. seealso:: - :ref:`pyramid:sessions_chapter`, - :ref:`pyramid:flash_messages`, and - :ref:`pyramid:session_module`. + :ref:`sessions_chapter`, + :ref:`flash_messages`, and + :ref:`session_module`. -- 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/sessions.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/quick_tutorial/sessions.rst') diff --git a/docs/quick_tutorial/sessions.rst b/docs/quick_tutorial/sessions.rst index b50d6d318..fad86fad4 100644 --- a/docs/quick_tutorial/sessions.rst +++ b/docs/quick_tutorial/sessions.rst @@ -31,8 +31,8 @@ Steps .. code-block:: bash - (env27)$ cd ..; cp -r view_classes sessions; cd sessions - (env27)$ python setup.py develop + (env)$ cd ..; cp -r view_classes sessions; cd sessions + (env)$ python setup.py develop #. Our ``sessions/tutorial/__init__.py`` needs a choice of session factory to get registered with the :term:`configurator`: @@ -56,13 +56,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/`` and ``http://localhost:6543/howdy`` in your browser. As you reload and switch between those URLs, note -- 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/sessions.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/quick_tutorial/sessions.rst') diff --git a/docs/quick_tutorial/sessions.rst b/docs/quick_tutorial/sessions.rst index fad86fad4..ef672891b 100644 --- a/docs/quick_tutorial/sessions.rst +++ b/docs/quick_tutorial/sessions.rst @@ -31,8 +31,8 @@ Steps .. code-block:: bash - (env)$ cd ..; cp -r view_classes sessions; cd sessions - (env)$ python setup.py develop + (venv)$ cd ..; cp -r view_classes sessions; cd sessions + (venv)$ python setup.py develop #. Our ``sessions/tutorial/__init__.py`` needs a choice of session factory to get registered with the :term:`configurator`: @@ -56,13 +56,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/`` and ``http://localhost:6543/howdy`` in your browser. As you reload and switch between those URLs, note -- 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/sessions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/quick_tutorial/sessions.rst') diff --git a/docs/quick_tutorial/sessions.rst b/docs/quick_tutorial/sessions.rst index ef672891b..4dd60079d 100644 --- a/docs/quick_tutorial/sessions.rst +++ b/docs/quick_tutorial/sessions.rst @@ -64,7 +64,7 @@ Steps (venv)$ pserve development.ini --reload -#. Open ``http://localhost:6543/`` and ``http://localhost:6543/howdy`` +#. Open http://localhost:6543/ and http://localhost:6543/howdy in your browser. As you reload and switch between those URLs, note that the counter increases and is *not* specific to the 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/sessions.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/quick_tutorial/sessions.rst') diff --git a/docs/quick_tutorial/sessions.rst b/docs/quick_tutorial/sessions.rst index 4dd60079d..56980f6ee 100644 --- a/docs/quick_tutorial/sessions.rst +++ b/docs/quick_tutorial/sessions.rst @@ -31,8 +31,8 @@ Steps .. code-block:: bash - (venv)$ cd ..; cp -r view_classes sessions; cd sessions - (venv)$ python setup.py develop + $ cd ..; cp -r view_classes sessions; cd sessions + $ $VENV/bin/python setup.py develop #. Our ``sessions/tutorial/__init__.py`` needs a choice of session factory to get registered with the :term:`configurator`: @@ -56,13 +56,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/ and http://localhost:6543/howdy in your browser. As you reload and switch between those URLs, note -- 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/sessions.rst | 2 ++ 1 file changed, 2 insertions(+) (limited to 'docs/quick_tutorial/sessions.rst') diff --git a/docs/quick_tutorial/sessions.rst b/docs/quick_tutorial/sessions.rst index 56980f6ee..ba26d0133 100644 --- a/docs/quick_tutorial/sessions.rst +++ b/docs/quick_tutorial/sessions.rst @@ -1,3 +1,5 @@ +.. _qtut_sessions: + ================================= 17: Transient Data Using Sessions ================================= -- cgit v1.2.3