File: sessions.rst

package info (click to toggle)
python-pyramid 1.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,112 kB
  • ctags: 8,169
  • sloc: python: 41,764; makefile: 111; sh: 17
file content (100 lines) | stat: -rw-r--r-- 3,054 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
.. _qtut_sessions:

=================================
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.  Third party packages such as
``pyramid_redis_sessions`` provide richer session support.  Or you can create
your own custom sessioning engine.  Let's take a look at the
:doc:`built-in sessioning support <../narr/sessions>`.

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

    $ 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`:

   .. 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

    $ $VENV/bin/nosetests tutorial

#. Run your Pyramid application with:

   .. code-block:: bash

    $ $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
   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 request.

Flash messages are a technique where messages can be stored between
requests, using sessions, then removed when they finally get displayed.

.. seealso::
   :ref:`sessions_chapter`,
   :ref:`flash_messages`, and
   :ref:`session_module`.