17: Transient Data Using 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

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.