12. Using session

12.1. Overview

Kay has a capability for handling anonymous session. Once you enable this capability, every request object has session attribute automatically. You can store any data(as long as it can be pickled) in this session.

12.2. Configuration

You need to put kay.sessions application into settings.INSTALLED_APPS and put kay.sessions.middleware.SessionMiddleware into settings.MIDDLEWARE_CLASSES. You can choose how to store session data by setting settings.SESSION_STORE value. The valid value is one of kay.sessions.sessionstore.GAESessionStore and kay.session.sessionstore.SecureCookieSessionStore.

SESSION_STORE = 'kay.sessions.sessionstore.GAESessionStore'
#SESSION_STORE = 'kay.sessions.sessionstore.SecureCookieSessionStore'


# ...


When using GAESessionStore, session data will be stored in Datastore, and only the session id will be passed to user’s browser as a cookie. When using SecureCookieSessionStore, all the session data will be stored in a single cookie in user’s browser, so you should store only small data as ids if you use SecureCookieSessionStore.

12.3. Decorator

Once kay has kay.sessions.decorators.no_session decorater, but you don’t need to use it any more. The attribute value of request.session is evaluated lazily, so there is no datastore/memcache access unless you use session’s value.

12.4. Purging old sessions

If you use GAESessionStore, you should delete expired old session data. There is a view for purging old sessions at kay.sessions.views.purge_old_sessions. This view is bound to the URL /sessions/purge_old_sessions by default. If you want to purge old session data, you need to call this view by cron or something similar periodically. Here is a sample cron settings for this:


- description: purge old session data
  url: /sessions/purge_old_sessions
  schedule: every 1 hours

12.5. How to store data into session

Treat request.session as if its just a dict. Here is an example view implementation of a simple counter.

def index(request):
  count = request.session.get('count', 0) + 1
  request.session['count'] = count

Table Of Contents

Previous topic

11. Using authentication

Next topic

13. Forms usage

This Page