1
0
mirror of https://github.com/django/django.git synced 2025-07-05 10:19:20 +00:00

magic-removal: Proofread docs/sessions.txt

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2788 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-04-29 02:57:58 +00:00
parent bacbec8600
commit ecd3d2e8c7

View File

@ -10,28 +10,30 @@ Cookies contain a session ID -- not the data itself.
Enabling sessions Enabling sessions
================= =================
Session functionality is enabled by default. Sessions are implemented via middleware_.
You can turn session functionality on and off by editing the Turn session functionality on and off by editing the ``MIDDLEWARE_CLASSES``
``MIDDLEWARE_CLASSES`` setting. To activate sessions, make sure setting. To activate sessions, make sure ``MIDDLEWARE_CLASSES`` contains
``MIDDLEWARE_CLASSES`` contains ``"django.contrib.sessions.middleware.SessionMiddleware"``. ``"django.contrib.sessions.middleware.SessionMiddleware"``.
The default ``settings.py`` created by ``django-admin.py startproject`` has
``SessionMiddleware`` activated.
If you don't want to use sessions, you might as well remove the If you don't want to use sessions, you might as well remove the
``SessionMiddleware`` line from ``MIDDLEWARE_CLASSES``. It'll save you a small ``SessionMiddleware`` line from ``MIDDLEWARE_CLASSES``. It'll save you a small
bit of overhead. bit of overhead.
.. _middleware: http://www.djangoproject.com/documentation/middleware/
Using sessions in views Using sessions in views
======================= =======================
Each ``HttpRequest`` object -- the first argument to any Django view function -- When ``SessionMiddleware`` is activated, each ``HttpRequest`` object -- the
has a ``session`` attribute, which is a dictionary-like object. You can read first argument to any Django view function -- will have a ``session``
it and write to it. attribute, which is a dictionary-like object. You can read it and write to it.
It implements the following standard dictionary methods: It implements the following standard dictionary methods:
* ``__contains__(key)``
Example: ``'fav_color' in request.session``
* ``__getitem__(key)`` * ``__getitem__(key)``
Example: ``fav_color = request.session['fav_color']`` Example: ``fav_color = request.session['fav_color']``
@ -42,6 +44,9 @@ It implements the following standard dictionary methods:
Example: ``del request.session['fav_color']``. This raises ``KeyError`` Example: ``del request.session['fav_color']``. This raises ``KeyError``
if the given ``key`` isn't already in the session. if the given ``key`` isn't already in the session.
* ``__contains__(key)``
Example: ``'fav_color' in request.session``
* ``get(key, default=None)`` * ``get(key, default=None)``
Example: ``fav_color = request.session.get('fav_color', 'red')`` Example: ``fav_color = request.session.get('fav_color', 'red')``