Sophie

Sophie

distrib > Arklinux > devel > i586 > media > main > by-pkgid > 5fcb1fedf34660bc240dc59b7bfcebc4 > files > 457

django-doc-1.2.3-1ark.noarch.rpm


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>How to use sessions &mdash; Django v1.2 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django v1.2 documentation" href="../../index.html" />
    <link rel="up" title="Handling HTTP requests" href="index.html" />
    <link rel="next" title="Working with forms" href="../forms/index.html" />
    <link rel="prev" title="Middleware" href="middleware.html" />
 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>

  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django v1.2 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="middleware.html" title="Middleware">previous</a> 
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="../forms/index.html" title="Working with forms">next</a> &raquo;</div>
    </div>
    
    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-http-sessions">
            
  <div class="section" id="s-module-django.contrib.sessions">
<span id="s-how-to-use-sessions"></span><span id="module-django.contrib.sessions"></span><span id="how-to-use-sessions"></span><h1>How to use sessions<a class="headerlink" href="#module-django.contrib.sessions" title="Permalink to this headline">¶</a></h1>
<p>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 &#8211; not the data itself.</p>
<div class="section" id="s-enabling-sessions">
<span id="enabling-sessions"></span><h2>Enabling sessions<a class="headerlink" href="#enabling-sessions" title="Permalink to this headline">¶</a></h2>
<p>Sessions are implemented via a piece of <a class="reference internal" href="../../ref/middleware.html"><em>middleware</em></a>.</p>
<p>To enable session functionality, do the following:</p>
<ul class="simple">
<li>Edit the <tt class="docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></tt> setting and make sure
<tt class="docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></tt> contains <tt class="docutils literal"><span class="pre">'django.contrib.sessions.middleware.SessionMiddleware'</span></tt>.
The default <tt class="docutils literal"><span class="pre">settings.py</span></tt> created by <tt class="docutils literal"><span class="pre">django-admin.py</span> <span class="pre">startproject</span></tt> has
<tt class="docutils literal"><span class="pre">SessionMiddleware</span></tt> activated.</li>
</ul>
<p>If you don&#8217;t want to use sessions, you might as well remove the
<tt class="docutils literal"><span class="pre">SessionMiddleware</span></tt> line from <tt class="docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></tt> and <tt class="docutils literal"><span class="pre">'django.contrib.sessions'</span></tt>
from your <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt>. It&#8217;ll save you a small bit of overhead.</p>
</div>
<div class="section" id="s-configuring-the-session-engine">
<span id="configuring-the-session-engine"></span><h2>Configuring the session engine<a class="headerlink" href="#configuring-the-session-engine" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>By default, Django stores sessions in your database (using the model
<tt class="docutils literal"><span class="pre">django.contrib.sessions.models.Session</span></tt>). Though this is convenient, in
some setups it&#8217;s faster to store session data elsewhere, so Django can be
configured to store session data on your filesystem or in your cache.</p>
<div class="section" id="s-using-database-backed-sessions">
<span id="using-database-backed-sessions"></span><h3>Using database-backed sessions<a class="headerlink" href="#using-database-backed-sessions" title="Permalink to this headline">¶</a></h3>
<p>If you want to use a database-backed session, you need to add
<tt class="docutils literal"><span class="pre">'django.contrib.sessions'</span></tt> to your <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt> setting.</p>
<p>Once you have configured your installation, run <tt class="docutils literal"><span class="pre">manage.py</span> <span class="pre">syncdb</span></tt>
to install the single database table that stores session data.</p>
</div>
<div class="section" id="s-using-cached-sessions">
<span id="using-cached-sessions"></span><h3>Using cached sessions<a class="headerlink" href="#using-cached-sessions" title="Permalink to this headline">¶</a></h3>
<p>For better performance, you may want to use a cache-based session backend.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.1:</span> Django 1.0 did not include the <tt class="docutils literal"><span class="pre">cached_db</span></tt> session backend.</div>
<p>To store session data using Django&#8217;s cache system, you&#8217;ll first need to make
sure you&#8217;ve configured your cache; see the <a class="reference internal" href="../cache.html"><em>cache documentation</em></a> for details.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">You should only use cache-based sessions if you&#8217;re using the Memcached
cache backend. The local-memory cache backend doesn&#8217;t retain data long
enough to be a good choice, and it&#8217;ll be faster to use file or database
sessions directly instead of sending everything through the file or
database cache backends.</p>
</div>
<p>Once your cache is configured, you&#8217;ve got two choices for how to store data in
the cache:</p>
<ul class="simple">
<li>Set <a class="reference internal" href="../../ref/settings.html#std:setting-SESSION_ENGINE"><tt class="xref std std-setting docutils literal"><span class="pre">SESSION_ENGINE</span></tt></a> to
<tt class="docutils literal"><span class="pre">&quot;django.contrib.sessions.backends.cache&quot;</span></tt> for a simple caching session
store. Session data will be stored directly your cache. However, session
data may not be persistent: cached data can be evicted if the cache fills
up or if the cache server is restarted.</li>
<li>For persistent, cached data, set <a class="reference internal" href="../../ref/settings.html#std:setting-SESSION_ENGINE"><tt class="xref std std-setting docutils literal"><span class="pre">SESSION_ENGINE</span></tt></a> to
<tt class="docutils literal"><span class="pre">&quot;django.contrib.sessions.backends.cached_db&quot;</span></tt>. This uses a
write-through cache &#8211; every write to the cache will also be written to
the database. Session reads only use the database if the data is not
already in the cache.</li>
</ul>
<p>Both session stores are quite fast, but the simple cache is faster because it
disregards persistence. In most cases, the <tt class="docutils literal"><span class="pre">cached_db</span></tt> backend will be fast
enough, but if you need that last bit of performance, and are willing to let
session data be expunged from time to time, the <tt class="docutils literal"><span class="pre">cache</span></tt> backend is for you.</p>
<p>If you use the <tt class="docutils literal"><span class="pre">cached_db</span></tt> session backend, you also need to follow the
configuration instructions for the <a class="reference internal" href="#using-database-backed-sessions">using database-backed sessions</a>.</p>
</div>
<div class="section" id="s-using-file-based-sessions">
<span id="using-file-based-sessions"></span><h3>Using file-based sessions<a class="headerlink" href="#using-file-based-sessions" title="Permalink to this headline">¶</a></h3>
<p>To use file-based sessions, set the <tt class="docutils literal"><span class="pre">SESSION_ENGINE</span></tt> setting to
<tt class="docutils literal"><span class="pre">&quot;django.contrib.sessions.backends.file&quot;</span></tt>.</p>
<p>You might also want to set the <tt class="docutils literal"><span class="pre">SESSION_FILE_PATH</span></tt> setting (which defaults
to output from <tt class="docutils literal"><span class="pre">tempfile.gettempdir()</span></tt>, most likely <tt class="docutils literal"><span class="pre">/tmp</span></tt>) to control
where Django stores session files. Be sure to check that your Web server has
permissions to read and write to this location.</p>
</div>
</div>
<div class="section" id="s-using-sessions-in-views">
<span id="using-sessions-in-views"></span><h2>Using sessions in views<a class="headerlink" href="#using-sessions-in-views" title="Permalink to this headline">¶</a></h2>
<p>When <tt class="docutils literal"><span class="pre">SessionMiddleware</span></tt> is activated, each <tt class="docutils literal"><span class="pre">HttpRequest</span></tt> object &#8211; the
first argument to any Django view function &#8211; will have a <tt class="docutils literal"><span class="pre">session</span></tt>
attribute, which is a dictionary-like object. You can read it and write to it.</p>
<p>A session object has the following standard dictionary methods:</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">__getitem__(key)</span></tt></p>
<p>Example: <tt class="docutils literal"><span class="pre">fav_color</span> <span class="pre">=</span> <span class="pre">request.session['fav_color']</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">__setitem__(key,</span> <span class="pre">value)</span></tt></p>
<p>Example: <tt class="docutils literal"><span class="pre">request.session['fav_color']</span> <span class="pre">=</span> <span class="pre">'blue'</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">__delitem__(key)</span></tt></p>
<p>Example: <tt class="docutils literal"><span class="pre">del</span> <span class="pre">request.session['fav_color']</span></tt>. This raises <tt class="docutils literal"><span class="pre">KeyError</span></tt>
if the given <tt class="docutils literal"><span class="pre">key</span></tt> isn&#8217;t already in the session.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">__contains__(key)</span></tt></p>
<p>Example: <tt class="docutils literal"><span class="pre">'fav_color'</span> <span class="pre">in</span> <span class="pre">request.session</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">get(key,</span> <span class="pre">default=None)</span></tt></p>
<p>Example: <tt class="docutils literal"><span class="pre">fav_color</span> <span class="pre">=</span> <span class="pre">request.session.get('fav_color',</span> <span class="pre">'red')</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">keys()</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">items()</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">setdefault()</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">clear()</span></tt></p>
</li>
</ul>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <tt class="docutils literal"><span class="pre">setdefault()</span></tt> and <tt class="docutils literal"><span class="pre">clear()</span></tt> are new in this version.</div>
<p>It also has these methods:</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">flush()</span></tt></p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>Delete the current session data from the session and regenerate the
session key value that is sent back to the user in the cookie. This is
used if you want to ensure that the previous session data can&#8217;t be
accessed again from the user&#8217;s browser (for example, the
<a class="reference internal" href="../auth.html#django.contrib.auth.logout" title="django.contrib.auth.logout"><tt class="xref py py-func docutils literal"><span class="pre">django.contrib.auth.logout()</span></tt></a> function calls it).</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">set_test_cookie()</span></tt></p>
<p>Sets a test cookie to determine whether the user&#8217;s browser supports
cookies. Due to the way cookies work, you won&#8217;t be able to test this
until the user&#8217;s next page request. See <a class="reference internal" href="#setting-test-cookies">Setting test cookies</a> below for
more information.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">test_cookie_worked()</span></tt></p>
<p>Returns either <tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt>, depending on whether the user&#8217;s
browser accepted the test cookie. Due to the way cookies work, you&#8217;ll
have to call <tt class="docutils literal"><span class="pre">set_test_cookie()</span></tt> on a previous, separate page request.
See <a class="reference internal" href="#setting-test-cookies">Setting test cookies</a> below for more information.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">delete_test_cookie()</span></tt></p>
<p>Deletes the test cookie. Use this to clean up after yourself.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">set_expiry(value)</span></tt></p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>Sets the expiration time for the session. You can pass a number of
different values:</p>
<ul class="simple">
<li>If <tt class="docutils literal"><span class="pre">value</span></tt> is an integer, the session will expire after that
many seconds of inactivity. For example, calling
<tt class="docutils literal"><span class="pre">request.session.set_expiry(300)</span></tt> would make the session expire
in 5 minutes.</li>
<li>If <tt class="docutils literal"><span class="pre">value</span></tt> is a <tt class="docutils literal"><span class="pre">datetime</span></tt> or <tt class="docutils literal"><span class="pre">timedelta</span></tt> object, the
session will expire at that specific date/time.</li>
<li>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">0</span></tt>, the user&#8217;s session cookie will expire
when the user&#8217;s Web browser is closed.</li>
<li>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="xref docutils literal"><span class="pre">None</span></tt>, the session reverts to using the global
session expiry policy.</li>
</ul>
<p>Reading a session is not considered activity for expiration
purposes. Session expiration is computed from the last time the
session was <em>modified</em>.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">get_expiry_age()</span></tt></p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>Returns the number of seconds until this session expires. For sessions
with no custom expiration (or those set to expire at browser close), this
will equal <tt class="docutils literal"><span class="pre">settings.SESSION_COOKIE_AGE</span></tt>.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">get_expiry_date()</span></tt></p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>Returns the date this session will expire. For sessions with no custom
expiration (or those set to expire at browser close), this will equal the
date <tt class="docutils literal"><span class="pre">settings.SESSION_COOKIE_AGE</span></tt> seconds from now.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">get_expire_at_browser_close()</span></tt></p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>Returns either <tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt>, depending on whether the user&#8217;s
session cookie will expire when the user&#8217;s Web browser is closed.</p>
</li>
</ul>
<p>You can edit <tt class="docutils literal"><span class="pre">request.session</span></tt> at any point in your view. You can edit it
multiple times.</p>
<div class="section" id="s-session-object-guidelines">
<span id="session-object-guidelines"></span><h3>Session object guidelines<a class="headerlink" href="#session-object-guidelines" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Use normal Python strings as dictionary keys on <tt class="docutils literal"><span class="pre">request.session</span></tt>. This
is more of a convention than a hard-and-fast rule.</li>
<li>Session dictionary keys that begin with an underscore are reserved for
internal use by Django.</li>
<li>Don&#8217;t override <tt class="docutils literal"><span class="pre">request.session</span></tt> with a new object, and don&#8217;t access or
set its attributes. Use it like a Python dictionary.</li>
</ul>
</div>
<div class="section" id="s-examples">
<span id="examples"></span><h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3>
<p>This simplistic view sets a <tt class="docutils literal"><span class="pre">has_commented</span></tt> variable to <tt class="xref docutils literal"><span class="pre">True</span></tt> after a user
posts a comment. It doesn&#8217;t let a user post a comment more than once:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">post_comment</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">new_comment</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;has_commented&#39;</span><span class="p">,</span> <span class="bp">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&quot;You&#39;ve already commented.&quot;</span><span class="p">)</span>
    <span class="n">c</span> <span class="o">=</span> <span class="n">comments</span><span class="o">.</span><span class="n">Comment</span><span class="p">(</span><span class="n">comment</span><span class="o">=</span><span class="n">new_comment</span><span class="p">)</span>
    <span class="n">c</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
    <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="p">[</span><span class="s">&#39;has_commented&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&#39;Thanks for your comment!&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>This simplistic view logs in a &quot;member&quot; of the site:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">login</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">Member</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s">&#39;username&#39;</span><span class="p">])</span>
    <span class="k">if</span> <span class="n">m</span><span class="o">.</span><span class="n">password</span> <span class="o">==</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s">&#39;password&#39;</span><span class="p">]:</span>
        <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="p">[</span><span class="s">&#39;member_id&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">id</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&quot;You&#39;re logged in.&quot;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&quot;Your username and password didn&#39;t match.&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>...And this one logs a member out, according to <tt class="docutils literal"><span class="pre">login()</span></tt> above:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">logout</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">del</span> <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="p">[</span><span class="s">&#39;member_id&#39;</span><span class="p">]</span>
    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&quot;You&#39;re logged out.&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>The standard <tt class="docutils literal"><span class="pre">django.contrib.auth.logout()</span></tt> function actually does a bit
more than this to prevent inadvertent data leakage. It calls
<tt class="docutils literal"><span class="pre">request.session.flush()</span></tt>. We are using this example as a demonstration of
how to work with session objects, not as a full <tt class="docutils literal"><span class="pre">logout()</span></tt> implementation.</p>
</div>
</div>
<div class="section" id="s-setting-test-cookies">
<span id="setting-test-cookies"></span><h2>Setting test cookies<a class="headerlink" href="#setting-test-cookies" title="Permalink to this headline">¶</a></h2>
<p>As a convenience, Django provides an easy way to test whether the user's
browser accepts cookies. Just call <tt class="docutils literal"><span class="pre">request.session.set_test_cookie()</span></tt> in a
view, and call <tt class="docutils literal"><span class="pre">request.session.test_cookie_worked()</span></tt> in a subsequent view --
not in the same view call.</p>
<p>This awkward split between <tt class="docutils literal"><span class="pre">set_test_cookie()</span></tt> and <tt class="docutils literal"><span class="pre">test_cookie_worked()</span></tt>
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.</p>
<p>It's good practice to use <tt class="docutils literal"><span class="pre">delete_test_cookie()</span></tt> to clean up after yourself.
Do this after you've verified that the test cookie worked.</p>
<p>Here's a typical usage example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">login</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&#39;POST&#39;</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">test_cookie_worked</span><span class="p">():</span>
            <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">delete_test_cookie</span><span class="p">()</span>
            <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&quot;You&#39;re logged in.&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&quot;Please enable cookies and try again.&quot;</span><span class="p">)</span>
    <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">set_test_cookie</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">&#39;foo/login_form.html&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-using-sessions-out-of-views">
<span id="using-sessions-out-of-views"></span><h2>Using sessions out of views<a class="headerlink" href="#using-sessions-out-of-views" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>An API is available to manipulate session data outside of a view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.contrib.sessions.backends.db</span> <span class="kn">import</span> <span class="n">SessionStore</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">SessionStore</span><span class="p">(</span><span class="n">session_key</span><span class="o">=</span><span class="s">&#39;2b1189a188b44ad18c35e113ac6ceead&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="p">[</span><span class="s">&#39;last_login&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2005</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">35</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="p">[</span><span class="s">&#39;last_login&#39;</span><span class="p">]</span>
<span class="go">datetime.datetime(2005, 8, 20, 13, 35, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
</pre></div>
</div>
<p>If you're using the <tt class="docutils literal"><span class="pre">django.contrib.sessions.backends.db</span></tt> backend, each
session is just a normal Django model. The <tt class="docutils literal"><span class="pre">Session</span></tt> model is defined in
<tt class="docutils literal"><span class="pre">django/contrib/sessions/models.py</span></tt>. Because it's a normal model, you can
access sessions using the normal Django database API:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.contrib.sessions.models</span> <span class="kn">import</span> <span class="n">Session</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">Session</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="s">&#39;2b1189a188b44ad18c35e113ac6ceead&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">expire_date</span>
<span class="go">datetime.datetime(2005, 8, 20, 13, 35, 12)</span>
</pre></div>
</div>
<p>Note that you'll need to call <tt class="docutils literal"><span class="pre">get_decoded()</span></tt> to get the session dictionary.
This is necessary because the dictionary is stored in an encoded format:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">session_data</span>
<span class="go">&#39;KGRwMQpTJ19hdXRoX3VzZXJfaWQnCnAyCkkxCnMuMTExY2ZjODI2Yj...&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">get_decoded</span><span class="p">()</span>
<span class="go">{&#39;user_id&#39;: 42}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-when-sessions-are-saved">
<span id="when-sessions-are-saved"></span><h2>When sessions are saved<a class="headerlink" href="#when-sessions-are-saved" title="Permalink to this headline">¶</a></h2>
<p>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:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Session is modified.</span>
<span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="p">[</span><span class="s">&#39;foo&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;bar&#39;</span>

<span class="c"># Session is modified.</span>
<span class="k">del</span> <span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="p">[</span><span class="s">&#39;foo&#39;</span><span class="p">]</span>

<span class="c"># Session is modified.</span>
<span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="p">[</span><span class="s">&#39;foo&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>

<span class="c"># Gotcha: Session is NOT modified, because this alters</span>
<span class="c"># request.session[&#39;foo&#39;] instead of request.session.</span>
<span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="p">[</span><span class="s">&#39;foo&#39;</span><span class="p">][</span><span class="s">&#39;bar&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;baz&#39;</span>
</pre></div>
</div>
<p>In the last case of the above example, we can tell the session object
explicitly that it has been modified by setting the <tt class="docutils literal"><span class="pre">modified</span></tt> attribute on
the session object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">request</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">modified</span> <span class="o">=</span> <span class="bp">True</span>
</pre></div>
</div>
<p>To change this default behavior, set the <tt class="docutils literal"><span class="pre">SESSION_SAVE_EVERY_REQUEST</span></tt> setting
to <tt class="xref docutils literal"><span class="pre">True</span></tt>. If <tt class="docutils literal"><span class="pre">SESSION_SAVE_EVERY_REQUEST</span></tt> is <tt class="xref docutils literal"><span class="pre">True</span></tt>, Django will save
the session to the database on every single request.</p>
<p>Note that the session cookie is only sent when a session has been created or
modified. If <tt class="docutils literal"><span class="pre">SESSION_SAVE_EVERY_REQUEST</span></tt> is <tt class="xref docutils literal"><span class="pre">True</span></tt>, the session cookie
will be sent on every request.</p>
<p>Similarly, the <tt class="docutils literal"><span class="pre">expires</span></tt> part of a session cookie is updated each time the
session cookie is sent.</p>
</div>
<div class="section" id="s-browser-length-sessions-vs-persistent-sessions">
<span id="browser-length-sessions-vs-persistent-sessions"></span><h2>Browser-length sessions vs. persistent sessions<a class="headerlink" href="#browser-length-sessions-vs-persistent-sessions" title="Permalink to this headline">¶</a></h2>
<p>You can control whether the session framework uses browser-length sessions vs.
persistent sessions with the <tt class="docutils literal"><span class="pre">SESSION_EXPIRE_AT_BROWSER_CLOSE</span></tt> setting.</p>
<p>By default, <tt class="docutils literal"><span class="pre">SESSION_EXPIRE_AT_BROWSER_CLOSE</span></tt> is set to <tt class="xref docutils literal"><span class="pre">False</span></tt>, which
means session cookies will be stored in users' browsers for as long as
<tt class="docutils literal"><span class="pre">SESSION_COOKIE_AGE</span></tt>. Use this if you don't want people to have to log in
every time they open a browser.</p>
<p>If <tt class="docutils literal"><span class="pre">SESSION_EXPIRE_AT_BROWSER_CLOSE</span></tt> is set to <tt class="xref docutils literal"><span class="pre">True</span></tt>, 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.</p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>This setting is a global default and can be overwritten at a per-session level
by explicitly calling <tt class="docutils literal"><span class="pre">request.session.set_expiry()</span></tt> as described above in
<a class="reference internal" href="#using-sessions-in-views">using sessions in views</a>.</p>
</div>
<div class="section" id="s-clearing-the-session-table">
<span id="clearing-the-session-table"></span><h2>Clearing the session table<a class="headerlink" href="#clearing-the-session-table" title="Permalink to this headline">¶</a></h2>
<p>If you're using the database backend, note that session data can accumulate in
the <tt class="docutils literal"><span class="pre">django_session</span></tt> database table and Django does <em>not</em> provide automatic
purging. Therefore, it's your job to purge expired sessions on a regular basis.</p>
<p>To understand this problem, consider what happens when a user uses a session.
When a user logs in, Django adds a row to the <tt class="docutils literal"><span class="pre">django_session</span></tt> 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 <em>not</em> log out,
the row never gets deleted.</p>
<p>Django provides a sample clean-up script: <tt class="docutils literal"><span class="pre">django-admin.py</span> <span class="pre">cleanup</span></tt>.
That script deletes any session in the session table whose <tt class="docutils literal"><span class="pre">expire_date</span></tt> is
in the past -- but your application may have different requirements.</p>
</div>
<div class="section" id="s-settings">
<span id="settings"></span><h2>Settings<a class="headerlink" href="#settings" title="Permalink to this headline">¶</a></h2>
<p>A few <a class="reference internal" href="../../ref/settings.html"><em>Django settings</em></a> give you control over session behavior:</p>
<div class="section" id="s-session-engine">
<span id="session-engine"></span><h3>SESSION_ENGINE<a class="headerlink" href="#session-engine" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<div class="versionchanged">
<span class="title">Changed in Django 1.1:</span> The <tt class="docutils literal"><span class="pre">cached_db</span></tt> backend was added</div>
<p>Default: <tt class="docutils literal"><span class="pre">django.contrib.sessions.backends.db</span></tt></p>
<p>Controls where Django stores session data. Valid values are:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">'django.contrib.sessions.backends.db'</span></tt></li>
<li><tt class="docutils literal"><span class="pre">'django.contrib.sessions.backends.file'</span></tt></li>
<li><tt class="docutils literal"><span class="pre">'django.contrib.sessions.backends.cache'</span></tt></li>
<li><tt class="docutils literal"><span class="pre">'django.contrib.sessions.backends.cached_db'</span></tt></li>
</ul>
<p>See <a class="reference internal" href="#configuring-the-session-engine">configuring the session engine</a> for more details.</p>
</div>
<div class="section" id="s-session-file-path">
<span id="session-file-path"></span><h3>SESSION_FILE_PATH<a class="headerlink" href="#session-file-path" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>Default: <tt class="docutils literal"><span class="pre">/tmp/</span></tt></p>
<p>If you're using file-based session storage, this sets the directory in
which Django will store session data.</p>
</div>
<div class="section" id="s-session-cookie-age">
<span id="session-cookie-age"></span><h3>SESSION_COOKIE_AGE<a class="headerlink" href="#session-cookie-age" title="Permalink to this headline">¶</a></h3>
<p>Default: <tt class="docutils literal"><span class="pre">1209600</span></tt> (2 weeks, in seconds)</p>
<p>The age of session cookies, in seconds.</p>
</div>
<div class="section" id="s-session-cookie-domain">
<span id="session-cookie-domain"></span><h3>SESSION_COOKIE_DOMAIN<a class="headerlink" href="#session-cookie-domain" title="Permalink to this headline">¶</a></h3>
<p>Default: <tt class="xref docutils literal"><span class="pre">None</span></tt></p>
<p>The domain to use for session cookies. Set this to a string such as
<tt class="docutils literal"><span class="pre">&quot;.lawrence.com&quot;</span></tt> (note the leading dot!) for cross-domain cookies, or use
<tt class="xref docutils literal"><span class="pre">None</span></tt> for a standard domain cookie.</p>
</div>
<div class="section" id="s-session-cookie-name">
<span id="session-cookie-name"></span><h3>SESSION_COOKIE_NAME<a class="headerlink" href="#session-cookie-name" title="Permalink to this headline">¶</a></h3>
<p>Default: <tt class="docutils literal"><span class="pre">'sessionid'</span></tt></p>
<p>The name of the cookie to use for sessions. This can be whatever you want.</p>
</div>
<div class="section" id="s-session-cookie-path">
<span id="session-cookie-path"></span><h3>SESSION_COOKIE_PATH<a class="headerlink" href="#session-cookie-path" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>Default: <tt class="docutils literal"><span class="pre">'/'</span></tt></p>
<p>The path set on the session cookie. This should either match the URL path of
your Django installation or be parent of that path.</p>
<p>This is useful if you have multiple Django instances running under the same
hostname. They can use different cookie paths, and each instance will only see
its own session cookie.</p>
</div>
<div class="section" id="s-session-cookie-secure">
<span id="session-cookie-secure"></span><h3>SESSION_COOKIE_SECURE<a class="headerlink" href="#session-cookie-secure" title="Permalink to this headline">¶</a></h3>
<p>Default: <tt class="xref docutils literal"><span class="pre">False</span></tt></p>
<p>Whether to use a secure cookie for the session cookie. If this is set to
<tt class="xref docutils literal"><span class="pre">True</span></tt>, the cookie will be marked as &quot;secure,&quot; which means browsers may
ensure that the cookie is only sent under an HTTPS connection.</p>
</div>
<div class="section" id="s-session-expire-at-browser-close">
<span id="session-expire-at-browser-close"></span><h3>SESSION_EXPIRE_AT_BROWSER_CLOSE<a class="headerlink" href="#session-expire-at-browser-close" title="Permalink to this headline">¶</a></h3>
<p>Default: <tt class="xref docutils literal"><span class="pre">False</span></tt></p>
<p>Whether to expire the session when the user closes his or her browser. See
&quot;Browser-length sessions vs. persistent sessions&quot; above.</p>
</div>
<div class="section" id="s-session-save-every-request">
<span id="session-save-every-request"></span><h3>SESSION_SAVE_EVERY_REQUEST<a class="headerlink" href="#session-save-every-request" title="Permalink to this headline">¶</a></h3>
<p>Default: <tt class="xref docutils literal"><span class="pre">False</span></tt></p>
<p>Whether to save the session data on every request. If this is <tt class="xref docutils literal"><span class="pre">False</span></tt>
(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.</p>
</div>
</div>
<div class="section" id="s-technical-details">
<span id="technical-details"></span><h2>Technical details<a class="headerlink" href="#technical-details" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>The session dictionary should accept any pickleable Python object. See
<a class="reference external" href="http://docs.python.org/library/pickle.html">the pickle module</a> for more information.</li>
<li>Session data is stored in a database table named <tt class="docutils literal"><span class="pre">django_session</span></tt> .</li>
<li>Django only sends a cookie if it needs to. If you don't set any session
data, it won't send a session cookie.</li>
</ul>
</div>
<div class="section" id="s-session-ids-in-urls">
<span id="session-ids-in-urls"></span><h2>Session IDs in URLs<a class="headerlink" href="#session-ids-in-urls" title="Permalink to this headline">¶</a></h2>
<p>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 &quot;Referer&quot;
header.</p>
</div>
</div>


          </div>         
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">How to use sessions</a><ul>
<li><a class="reference internal" href="#enabling-sessions">Enabling sessions</a></li>
<li><a class="reference internal" href="#configuring-the-session-engine">Configuring the session engine</a><ul>
<li><a class="reference internal" href="#using-database-backed-sessions">Using database-backed sessions</a></li>
<li><a class="reference internal" href="#using-cached-sessions">Using cached sessions</a></li>
<li><a class="reference internal" href="#using-file-based-sessions">Using file-based sessions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-sessions-in-views">Using sessions in views</a><ul>
<li><a class="reference internal" href="#session-object-guidelines">Session object guidelines</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
<li><a class="reference internal" href="#setting-test-cookies">Setting test cookies</a></li>
<li><a class="reference internal" href="#using-sessions-out-of-views">Using sessions out of views</a></li>
<li><a class="reference internal" href="#when-sessions-are-saved">When sessions are saved</a></li>
<li><a class="reference internal" href="#browser-length-sessions-vs-persistent-sessions">Browser-length sessions vs. persistent sessions</a></li>
<li><a class="reference internal" href="#clearing-the-session-table">Clearing the session table</a></li>
<li><a class="reference internal" href="#settings">Settings</a><ul>
<li><a class="reference internal" href="#session-engine">SESSION_ENGINE</a></li>
<li><a class="reference internal" href="#session-file-path">SESSION_FILE_PATH</a></li>
<li><a class="reference internal" href="#session-cookie-age">SESSION_COOKIE_AGE</a></li>
<li><a class="reference internal" href="#session-cookie-domain">SESSION_COOKIE_DOMAIN</a></li>
<li><a class="reference internal" href="#session-cookie-name">SESSION_COOKIE_NAME</a></li>
<li><a class="reference internal" href="#session-cookie-path">SESSION_COOKIE_PATH</a></li>
<li><a class="reference internal" href="#session-cookie-secure">SESSION_COOKIE_SECURE</a></li>
<li><a class="reference internal" href="#session-expire-at-browser-close">SESSION_EXPIRE_AT_BROWSER_CLOSE</a></li>
<li><a class="reference internal" href="#session-save-every-request">SESSION_SAVE_EVERY_REQUEST</a></li>
</ul>
</li>
<li><a class="reference internal" href="#technical-details">Technical details</a></li>
<li><a class="reference internal" href="#session-ids-in-urls">Session IDs in URLs</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="middleware.html">Middleware</a></li>
    
    
      <li>Next: <a href="../forms/index.html">Working with forms</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django v1.2 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
          <ul><li><a href="index.html">Handling HTTP requests</a>
        
        <ul><li>How to use sessions</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>  

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/topics/http/sessions.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Oct 20, 2010</p>
          </div> 
        
      
    </div>
    
    <div id="ft">
      <div class="nav">
    &laquo; <a href="middleware.html" title="Middleware">previous</a> 
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="../forms/index.html" title="Working with forms">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>