2005-08-17 12:00:02 +08:00
|
|
|
===================
|
|
|
|
How to use sessions
|
|
|
|
===================
|
|
|
|
|
|
|
|
Django provides full support for anonymous sessions. The session framework lets
|
|
|
|
you store and retrieve arbitrary data on a per-site-visitor basis. It stores
|
|
|
|
data on the server side and abstracts the sending and receiving of cookies.
|
|
|
|
Cookies contain a session ID -- not the data itself.
|
|
|
|
|
|
|
|
Enabling sessions
|
|
|
|
=================
|
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
Sessions are implemented via a piece of middleware_.
|
2005-08-17 12:00:02 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
To enable session functionality, do the following:
|
2005-08-17 12:00:02 +08:00
|
|
|
|
2006-07-08 06:25:32 +08:00
|
|
|
* Edit the ``MIDDLEWARE_CLASSES`` setting and make sure
|
|
|
|
``MIDDLEWARE_CLASSES`` contains ``'django.contrib.sessions.middleware.SessionMiddleware'``.
|
|
|
|
The default ``settings.py`` created by ``django-admin.py startproject`` has
|
|
|
|
``SessionMiddleware`` activated.
|
2007-09-16 06:36:53 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
* Add ``'django.contrib.sessions'`` to your ``INSTALLED_APPS`` setting,
|
|
|
|
and run ``manage.py syncdb`` to install the single database table
|
|
|
|
that stores session data.
|
2007-09-16 06:36:53 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
**New in development version**: this step is optional if you're not using
|
2007-09-16 06:36:53 +08:00
|
|
|
the database session backend; see `configuring the session engine`_.
|
2005-08-17 12:00:02 +08:00
|
|
|
|
|
|
|
If you don't want to use sessions, you might as well remove the
|
2006-07-08 06:25:32 +08:00
|
|
|
``SessionMiddleware`` line from ``MIDDLEWARE_CLASSES`` and ``'django.contrib.sessions'``
|
|
|
|
from your ``INSTALLED_APPS``. It'll save you a small bit of overhead.
|
2005-08-17 12:00:02 +08:00
|
|
|
|
2007-01-25 04:08:47 +08:00
|
|
|
.. _middleware: ../middleware/
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
Configuring the session engine
|
|
|
|
==============================
|
|
|
|
|
|
|
|
**New in development version**.
|
|
|
|
|
|
|
|
By default, Django stores sessions in your database (using the model
|
|
|
|
``django.contrib.sessions.models.Session``). Though this is convenient, in
|
|
|
|
some setups it's faster to store session data elsewhere, so Django can be
|
|
|
|
configured to store session data on your filesystem or in your cache.
|
|
|
|
|
|
|
|
Using file-based sessions
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
To use file-based sessions, set the ``SESSION_ENGINE`` setting to
|
|
|
|
``"django.contrib.sessions.backends.file"``.
|
|
|
|
|
|
|
|
You might also want to set the ``SESSION_FILE_PATH`` setting (which
|
|
|
|
defaults to ``/tmp``) to control where Django stores session files. Be
|
2007-09-16 06:36:53 +08:00
|
|
|
sure to check that your Web server has permissions to read and write to
|
2007-09-16 05:29:14 +08:00
|
|
|
this location.
|
|
|
|
|
|
|
|
Using cache-based sessions
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
To store session data using Django's cache system, set ``SESSION_ENGINE``
|
|
|
|
to ``"django.contrib.sessions.backends.cache"``. You'll want to make sure
|
|
|
|
you've configured your cache; see the `cache documentation`_ for details.
|
|
|
|
|
|
|
|
.. _cache documentation: ../cache/
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2007-09-16 06:36:53 +08:00
|
|
|
You should probably only use cache-based sessions if you're using the
|
|
|
|
memcached cache backend. The local memory and simple cache backends
|
2007-09-16 05:29:14 +08:00
|
|
|
don't retain data long enough to be good choices, and it'll be faster
|
|
|
|
to use file or database sessions directly instead of sending everything
|
|
|
|
through the file or database cache backends.
|
|
|
|
|
2005-08-17 12:00:02 +08:00
|
|
|
Using sessions in views
|
|
|
|
=======================
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
When ``SessionMiddleware`` is activated, each ``HttpRequest`` object -- the
|
|
|
|
first argument to any Django view function -- will have a ``session``
|
|
|
|
attribute, which is a dictionary-like object. You can read it and write to it.
|
2005-08-17 12:00:02 +08:00
|
|
|
|
|
|
|
It implements the following standard dictionary methods:
|
|
|
|
|
|
|
|
* ``__getitem__(key)``
|
|
|
|
Example: ``fav_color = request.session['fav_color']``
|
|
|
|
|
|
|
|
* ``__setitem__(key, value)``
|
|
|
|
Example: ``request.session['fav_color'] = 'blue'``
|
|
|
|
|
|
|
|
* ``__delitem__(key)``
|
2005-11-21 01:16:13 +08:00
|
|
|
Example: ``del request.session['fav_color']``. This raises ``KeyError``
|
|
|
|
if the given ``key`` isn't already in the session.
|
2005-08-17 12:00:02 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
* ``__contains__(key)``
|
|
|
|
Example: ``'fav_color' in request.session``
|
|
|
|
|
2005-08-17 12:00:02 +08:00
|
|
|
* ``get(key, default=None)``
|
|
|
|
Example: ``fav_color = request.session.get('fav_color', 'red')``
|
|
|
|
|
2006-02-11 05:33:07 +08:00
|
|
|
* ``keys()``
|
|
|
|
|
|
|
|
* ``items()``
|
|
|
|
|
2007-12-02 23:27:29 +08:00
|
|
|
* ``setdefault()``
|
|
|
|
|
2005-09-23 09:28:44 +08:00
|
|
|
It also has these three methods:
|
2005-08-17 12:00:02 +08:00
|
|
|
|
|
|
|
* ``set_test_cookie()``
|
|
|
|
Sets a test cookie to determine whether the user's browser supports
|
|
|
|
cookies. Due to the way cookies work, you won't be able to test this
|
|
|
|
until the user's next page request. See "Setting test cookies" below for
|
|
|
|
more information.
|
|
|
|
|
|
|
|
* ``test_cookie_worked()``
|
|
|
|
Returns either ``True`` or ``False``, depending on whether the user's
|
|
|
|
browser accepted the test cookie. Due to the way cookies work, you'll
|
|
|
|
have to call ``set_test_cookie()`` on a previous, separate page request.
|
|
|
|
See "Setting test cookies" below for more information.
|
|
|
|
|
2005-09-23 09:28:44 +08:00
|
|
|
* ``delete_test_cookie()``
|
|
|
|
Deletes the test cookie. Use this to clean up after yourself.
|
|
|
|
|
2005-08-17 12:00:02 +08:00
|
|
|
You can edit ``request.session`` at any point in your view. You can edit it
|
|
|
|
multiple times.
|
|
|
|
|
2005-08-17 12:05:42 +08:00
|
|
|
Session object guidelines
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
* Use normal Python strings as dictionary keys on ``request.session``. This
|
|
|
|
is more of a convention than a hard-and-fast rule.
|
|
|
|
|
|
|
|
* Session dictionary keys that begin with an underscore are reserved for
|
|
|
|
internal use by Django.
|
|
|
|
|
|
|
|
* Don't override ``request.session`` with a new object, and don't access or
|
|
|
|
set its attributes. Use it like a Python dictionary.
|
|
|
|
|
2005-08-17 12:00:02 +08:00
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
|
|
|
|
This simplistic view sets a ``has_commented`` variable to ``True`` after a user
|
|
|
|
posts a comment. It doesn't let a user post a comment more than once::
|
|
|
|
|
|
|
|
def post_comment(request, new_comment):
|
|
|
|
if request.session.get('has_commented', False):
|
|
|
|
return HttpResponse("You've already commented.")
|
|
|
|
c = comments.Comment(comment=new_comment)
|
|
|
|
c.save()
|
|
|
|
request.session['has_commented'] = True
|
|
|
|
return HttpResponse('Thanks for your comment!')
|
|
|
|
|
2005-11-13 12:43:07 +08:00
|
|
|
This simplistic view logs in a "member" of the site::
|
2005-08-17 12:00:02 +08:00
|
|
|
|
|
|
|
def login(request):
|
2007-05-03 06:10:12 +08:00
|
|
|
m = Member.objects.get(username=request.POST['username'])
|
2005-11-13 12:43:07 +08:00
|
|
|
if m.password == request.POST['password']:
|
|
|
|
request.session['member_id'] = m.id
|
2005-08-17 12:00:02 +08:00
|
|
|
return HttpResponse("You're logged in.")
|
|
|
|
else:
|
|
|
|
return HttpResponse("Your username and password didn't match.")
|
|
|
|
|
2005-11-13 12:43:07 +08:00
|
|
|
...And this one logs a member out, according to ``login()`` above::
|
2005-08-17 12:00:02 +08:00
|
|
|
|
|
|
|
def logout(request):
|
|
|
|
try:
|
2005-11-13 12:43:07 +08:00
|
|
|
del request.session['member_id']
|
2005-08-17 12:00:02 +08:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
return HttpResponse("You're logged out.")
|
|
|
|
|
|
|
|
Setting test cookies
|
|
|
|
====================
|
|
|
|
|
|
|
|
As a convenience, Django provides an easy way to test whether the user's
|
|
|
|
browser accepts cookies. Just call ``request.session.set_test_cookie()`` in a
|
|
|
|
view, and call ``request.session.test_cookie_worked()`` in a subsequent view --
|
|
|
|
not in the same view call.
|
|
|
|
|
|
|
|
This awkward split between ``set_test_cookie()`` and ``test_cookie_worked()``
|
|
|
|
is necessary due to the way cookies work. When you set a cookie, you can't
|
|
|
|
actually tell whether a browser accepted it until the browser's next request.
|
|
|
|
|
2005-09-23 09:28:44 +08:00
|
|
|
It's good practice to use ``delete_test_cookie()`` to clean up after yourself.
|
|
|
|
Do this after you've verified that the test cookie worked.
|
|
|
|
|
2005-08-17 12:00:02 +08:00
|
|
|
Here's a typical usage example::
|
|
|
|
|
|
|
|
def login(request):
|
2006-11-27 08:08:46 +08:00
|
|
|
if request.method == 'POST':
|
2005-08-17 12:00:02 +08:00
|
|
|
if request.session.test_cookie_worked():
|
2005-09-23 09:28:44 +08:00
|
|
|
request.session.delete_test_cookie()
|
2005-08-17 12:00:02 +08:00
|
|
|
return HttpResponse("You're logged in.")
|
|
|
|
else:
|
|
|
|
return HttpResponse("Please enable cookies and try again.")
|
|
|
|
request.session.set_test_cookie()
|
2006-05-02 09:31:56 +08:00
|
|
|
return render_to_response('foo/login_form.html')
|
2005-08-17 12:00:02 +08:00
|
|
|
|
|
|
|
Using sessions out of views
|
|
|
|
===========================
|
|
|
|
|
2007-09-16 06:36:53 +08:00
|
|
|
**New in Django development version**
|
|
|
|
|
|
|
|
An API is available to manipulate session data outside of a view::
|
2007-09-16 05:29:14 +08:00
|
|
|
|
2007-09-20 07:32:53 +08:00
|
|
|
>>> from django.contrib.sessions.backends.db import SessionStore
|
2007-09-16 05:29:14 +08:00
|
|
|
>>> s = SessionStore(session_key='2b1189a188b44ad18c35e113ac6ceead')
|
|
|
|
>>> s['last_login'] = datetime.datetime(2005, 8, 20, 13, 35, 10)
|
|
|
|
>>> s['last_login']
|
|
|
|
datetime.datetime(2005, 8, 20, 13, 35, 0)
|
|
|
|
>>> s.save()
|
|
|
|
|
2007-09-20 10:02:21 +08:00
|
|
|
If you're using the ``django.contrib.sessions.backends.db`` backend, each
|
2007-09-16 06:36:53 +08:00
|
|
|
session is just a normal Django model. The ``Session`` model is defined in
|
|
|
|
``django/contrib/sessions/models.py``. Because it's a normal model, you can
|
|
|
|
access sessions using the normal Django database API::
|
2005-08-17 12:00:02 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> from django.contrib.sessions.models import Session
|
2007-03-26 07:22:04 +08:00
|
|
|
>>> s = Session.objects.get(pk='2b1189a188b44ad18c35e113ac6ceead')
|
2005-08-17 12:00:02 +08:00
|
|
|
>>> s.expire_date
|
2007-09-16 06:36:53 +08:00
|
|
|
datetime.datetime(2005, 8, 20, 13, 35, 12)
|
2005-08-17 12:00:02 +08:00
|
|
|
|
|
|
|
Note that you'll need to call ``get_decoded()`` to get the session dictionary.
|
|
|
|
This is necessary because the dictionary is stored in an encoded format::
|
|
|
|
|
|
|
|
>>> s.session_data
|
|
|
|
'KGRwMQpTJ19hdXRoX3VzZXJfaWQnCnAyCkkxCnMuMTExY2ZjODI2Yj...'
|
|
|
|
>>> s.get_decoded()
|
|
|
|
{'user_id': 42}
|
|
|
|
|
2005-11-21 01:16:13 +08:00
|
|
|
When sessions are saved
|
|
|
|
=======================
|
|
|
|
|
|
|
|
By default, Django only saves to the session database when the session has been
|
|
|
|
modified -- that is if any of its dictionary values have been assigned or
|
|
|
|
deleted::
|
|
|
|
|
|
|
|
# Session is modified.
|
|
|
|
request.session['foo'] = 'bar'
|
|
|
|
|
|
|
|
# Session is modified.
|
|
|
|
del request.session['foo']
|
|
|
|
|
|
|
|
# Session is modified.
|
|
|
|
request.session['foo'] = {}
|
|
|
|
|
|
|
|
# Gotcha: Session is NOT modified, because this alters
|
|
|
|
# request.session['foo'] instead of request.session.
|
|
|
|
request.session['foo']['bar'] = 'baz'
|
|
|
|
|
2007-08-12 18:24:21 +08:00
|
|
|
In the last case of the above example, we can tell the session object
|
|
|
|
explicitly that it has been modified by setting the ``modified`` attribute on
|
|
|
|
the session object::
|
|
|
|
|
|
|
|
request.session.modified = True
|
|
|
|
|
2006-01-11 08:11:29 +08:00
|
|
|
To change this default behavior, set the ``SESSION_SAVE_EVERY_REQUEST`` setting
|
|
|
|
to ``True``. If ``SESSION_SAVE_EVERY_REQUEST`` is ``True``, Django will save
|
|
|
|
the session to the database on every single request.
|
2005-11-02 22:26:55 +08:00
|
|
|
|
2005-11-21 01:16:13 +08:00
|
|
|
Note that the session cookie is only sent when a session has been created or
|
|
|
|
modified. If ``SESSION_SAVE_EVERY_REQUEST`` is ``True``, the session cookie
|
|
|
|
will be sent on every request.
|
|
|
|
|
|
|
|
Similarly, the ``expires`` part of a session cookie is updated each time the
|
|
|
|
session cookie is sent.
|
|
|
|
|
2006-06-02 06:25:06 +08:00
|
|
|
Browser-length sessions vs. persistent sessions
|
|
|
|
===============================================
|
|
|
|
|
|
|
|
You can control whether the session framework uses browser-length sessions vs.
|
|
|
|
persistent sessions with the ``SESSION_EXPIRE_AT_BROWSER_CLOSE`` setting.
|
|
|
|
|
|
|
|
By default, ``SESSION_EXPIRE_AT_BROWSER_CLOSE`` is set to ``False``, which
|
|
|
|
means session cookies will be stored in users' browsers for as long as
|
|
|
|
``SESSION_COOKIE_AGE``. Use this if you don't want people to have to log in
|
|
|
|
every time they open a browser.
|
|
|
|
|
|
|
|
If ``SESSION_EXPIRE_AT_BROWSER_CLOSE`` is set to ``True``, Django will use
|
|
|
|
browser-length cookies -- cookies that expire as soon as the user closes his or
|
|
|
|
her browser. Use this if you want people to have to log in every time they open
|
|
|
|
a browser.
|
|
|
|
|
2006-12-30 15:30:00 +08:00
|
|
|
Clearing the session table
|
|
|
|
==========================
|
|
|
|
|
|
|
|
Note that session data can accumulate in the ``django_session`` database table
|
|
|
|
and Django does *not* provide automatic purging. Therefore, it's your job to
|
|
|
|
purge expired sessions on a regular basis.
|
|
|
|
|
|
|
|
To understand this problem, consider what happens when a user uses a session.
|
|
|
|
When a user logs in, Django adds a row to the ``django_session`` database
|
|
|
|
table. Django updates this row each time the session data changes. If the user
|
|
|
|
logs out manually, Django deletes the row. But if the user does *not* log out,
|
|
|
|
the row never gets deleted.
|
|
|
|
|
|
|
|
Django provides a sample clean-up script in ``django/bin/daily_cleanup.py``.
|
|
|
|
That script deletes any session in the session table whose ``expire_date`` is
|
|
|
|
in the past -- but your application may have different requirements.
|
|
|
|
|
2005-11-21 01:16:13 +08:00
|
|
|
Settings
|
|
|
|
========
|
|
|
|
|
|
|
|
A few `Django settings`_ give you control over session behavior:
|
2005-11-02 22:26:55 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
SESSION_ENGINE
|
|
|
|
--------------
|
|
|
|
|
|
|
|
**New in Django development version**
|
|
|
|
|
|
|
|
Default: ``django.contrib.sessions.backends.db``
|
|
|
|
|
|
|
|
Controls where Django stores session data. Valid values are:
|
|
|
|
|
2007-09-16 06:36:53 +08:00
|
|
|
* ``'django.contrib.sessions.backends.db'``
|
|
|
|
* ``'django.contrib.sessions.backends.file'``
|
2007-09-16 05:29:14 +08:00
|
|
|
* ``'django.contrib.sessions.backends.cache'``
|
2007-09-16 06:36:53 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
See `configuring the session engine`_ for more details.
|
|
|
|
|
|
|
|
SESSION_FILE_PATH
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
**New in Django development version**
|
|
|
|
|
|
|
|
Default: ``/tmp/``
|
|
|
|
|
|
|
|
If you're using file-based session storage, this sets the directory in
|
|
|
|
which Django will store session data.
|
|
|
|
|
2005-11-02 22:26:55 +08:00
|
|
|
SESSION_COOKIE_AGE
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Default: ``1209600`` (2 weeks, in seconds)
|
|
|
|
|
|
|
|
The age of session cookies, in seconds.
|
|
|
|
|
|
|
|
SESSION_COOKIE_DOMAIN
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
Default: ``None``
|
|
|
|
|
|
|
|
The domain to use for session cookies. Set this to a string such as
|
|
|
|
``".lawrence.com"`` for cross-domain cookies, or use ``None`` for a standard
|
|
|
|
domain cookie.
|
|
|
|
|
|
|
|
SESSION_COOKIE_NAME
|
|
|
|
-------------------
|
|
|
|
|
2006-01-11 08:11:29 +08:00
|
|
|
Default: ``'sessionid'``
|
2005-11-02 22:26:55 +08:00
|
|
|
|
|
|
|
The name of the cookie to use for sessions. This can be whatever you want.
|
|
|
|
|
2006-08-12 14:02:28 +08:00
|
|
|
SESSION_COOKIE_SECURE
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
Default: ``False``
|
|
|
|
|
|
|
|
Whether to use a secure cookie for the session cookie. If this is set to
|
|
|
|
``True``, the cookie will be marked as "secure," which means browsers may
|
|
|
|
ensure that the cookie is only sent under an HTTPS connection.
|
|
|
|
|
2006-06-02 06:25:06 +08:00
|
|
|
SESSION_EXPIRE_AT_BROWSER_CLOSE
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
Default: ``False``
|
|
|
|
|
|
|
|
Whether to expire the session when the user closes his or her browser. See
|
|
|
|
"Browser-length sessions vs. persistent sessions" above.
|
|
|
|
|
2005-11-21 01:16:13 +08:00
|
|
|
SESSION_SAVE_EVERY_REQUEST
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
Default: ``False``
|
|
|
|
|
|
|
|
Whether to save the session data on every request. If this is ``False``
|
|
|
|
(default), then the session data will only be saved if it has been modified --
|
|
|
|
that is, if any of its dictionary values have been assigned or deleted.
|
|
|
|
|
2007-01-25 04:08:47 +08:00
|
|
|
.. _Django settings: ../settings/
|
2005-11-02 22:26:55 +08:00
|
|
|
|
2005-08-17 12:00:02 +08:00
|
|
|
Technical details
|
|
|
|
=================
|
|
|
|
|
|
|
|
* The session dictionary should accept any pickleable Python object. See
|
|
|
|
`the pickle module`_ for more information.
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
* Session data is stored in a database table named ``django_session`` .
|
2005-08-17 12:00:02 +08:00
|
|
|
|
|
|
|
* Django only sends a cookie if it needs to. If you don't set any session
|
|
|
|
data, it won't send a session cookie.
|
|
|
|
|
|
|
|
.. _`the pickle module`: http://www.python.org/doc/current/lib/module-pickle.html
|
2005-11-02 22:26:55 +08:00
|
|
|
|
|
|
|
Session IDs in URLs
|
|
|
|
===================
|
|
|
|
|
|
|
|
The Django sessions framework is entirely, and solely, cookie-based. It does
|
|
|
|
not fall back to putting session IDs in URLs as a last resort, as PHP does.
|
|
|
|
This is an intentional design decision. Not only does that behavior make URLs
|
|
|
|
ugly, it makes your site vulnerable to session-ID theft via the "Referer"
|
|
|
|
header.
|