Sophie

Sophie

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

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>Django at a glance &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="Getting started" href="index.html" />
    <link rel="next" title="Quick install guide" href="install.html" />
    <link rel="prev" title="Getting started" href="index.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="index.html" title="Getting started">previous</a> 
     |
    <a href="index.html" title="Getting started" accesskey="U">up</a>
   |
    <a href="install.html" title="Quick install guide">next</a> &raquo;</div>
    </div>
    
    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="intro-overview">
            
  <div class="section" id="s-django-at-a-glance">
<span id="django-at-a-glance"></span><h1>Django at a glance<a class="headerlink" href="#django-at-a-glance" title="Permalink to this headline">¶</a></h1>
<p>Because Django was developed in a fast-paced newsroom environment, it was
designed to make common Web-development tasks fast and easy. Here&#8217;s an informal
overview of how to write a database-driven Web app with Django.</p>
<p>The goal of this document is to give you enough technical specifics to
understand how Django works, but this isn&#8217;t intended to be a tutorial or
reference &#8211; but we&#8217;ve got both! When you&#8217;re ready to start a project, you can
<a class="reference internal" href="tutorial01.html"><em>start with the tutorial</em></a> or <a class="reference internal" href="../topics/index.html"><em>dive right into more
detailed documentation</em></a>.</p>
<div class="section" id="s-design-your-model">
<span id="design-your-model"></span><h2>Design your model<a class="headerlink" href="#design-your-model" title="Permalink to this headline">¶</a></h2>
<p>Although you can use Django without a database, it comes with an
object-relational mapper in which you describe your database layout in Python
code.</p>
<p>The <a class="reference internal" href="../topics/db/models.html"><em>data-model syntax</em></a> offers many rich ways of
representing your models &#8211; so far, it&#8217;s been solving two years&#8217; worth of
database-schema problems. Here&#8217;s a quick example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Reporter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">full_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">70</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">full_name</span>

<span class="k">class</span> <span class="nc">Article</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">()</span>
    <span class="n">headline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
    <span class="n">content</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
    <span class="n">reporter</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Reporter</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">headline</span>
</pre></div>
</div>
</div>
<div class="section" id="s-install-it">
<span id="install-it"></span><h2>Install it<a class="headerlink" href="#install-it" title="Permalink to this headline">¶</a></h2>
<p>Next, run the Django command-line utility to create the database tables
automatically:</p>
<div class="highlight-bash"><div class="highlight"><pre>manage.py syncdb
</pre></div>
</div>
<p>The <a class="reference internal" href="../ref/django-admin.html#django-admin-syncdb"><tt class="xref std std-djadmin docutils literal"><span class="pre">syncdb</span></tt></a> command looks at all your available models and creates
tables in your database for whichever tables don't already exist.</p>
</div>
<div class="section" id="s-enjoy-the-free-api">
<span id="enjoy-the-free-api"></span><h2>Enjoy the free API<a class="headerlink" href="#enjoy-the-free-api" title="Permalink to this headline">¶</a></h2>
<p>With that, you've got a free, and rich, <a class="reference internal" href="../topics/db/queries.html"><em>Python API</em></a> to
access your data. The API is created on the fly, no code generation necessary:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mysite.models</span> <span class="kn">import</span> <span class="n">Reporter</span><span class="p">,</span> <span class="n">Article</span>

<span class="go"># No reporters are in the system yet.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Reporter</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="go">[]</span>

<span class="go"># Create a new Reporter.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">Reporter</span><span class="p">(</span><span class="n">full_name</span><span class="o">=</span><span class="s">&#39;John Smith&#39;</span><span class="p">)</span>

<span class="go"># Save the object into the database. You have to call save() explicitly.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>

<span class="go"># Now it has an ID.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">id</span>
<span class="go">1</span>

<span class="go"># Now the new reporter is in the database.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Reporter</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="go">[&lt;Reporter: John Smith&gt;]</span>

<span class="go"># Fields are represented as attributes on the Python object.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">full_name</span>
<span class="go">&#39;John Smith&#39;</span>

<span class="go"># Django provides a rich database lookup API.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Reporter</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="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">&lt;Reporter: John Smith&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Reporter</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">full_name__startswith</span><span class="o">=</span><span class="s">&#39;John&#39;</span><span class="p">)</span>
<span class="go">&lt;Reporter: John Smith&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Reporter</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">full_name__contains</span><span class="o">=</span><span class="s">&#39;mith&#39;</span><span class="p">)</span>
<span class="go">&lt;Reporter: John Smith&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Reporter</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="nb">id</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="nc">DoesNotExist</span>: <span class="n-Identifier">Reporter matching query does not exist.</span>

<span class="go"># Create an article.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Article</span><span class="p">(</span><span class="n">pub_date</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(),</span> <span class="n">headline</span><span class="o">=</span><span class="s">&#39;Django is cool&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">content</span><span class="o">=</span><span class="s">&#39;Yeah.&#39;</span><span class="p">,</span> <span class="n">reporter</span><span class="o">=</span><span class="n">r</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>

<span class="go"># Now the article is in the database.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="go">[&lt;Article: Django is cool&gt;]</span>

<span class="go"># Article objects get API access to related Reporter objects.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">reporter</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">full_name</span>
<span class="go">&#39;John Smith&#39;</span>

<span class="go"># And vice versa: Reporter objects get API access to Article objects.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">article_set</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="go">[&lt;Article: Django is cool&gt;]</span>

<span class="go"># The API follows relationships as far as you need, performing efficient</span>
<span class="go"># JOINs for you behind the scenes.</span>
<span class="go"># This finds all articles by a reporter whose name starts with &quot;John&quot;.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">reporter__full_name__startswith</span><span class="o">=</span><span class="s">&quot;John&quot;</span><span class="p">)</span>
<span class="go">[&lt;Article: Django is cool&gt;]</span>

<span class="go"># Change an object by altering its attributes and calling save().</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">full_name</span> <span class="o">=</span> <span class="s">&#39;Billy Goat&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>

<span class="go"># Delete an object with delete().</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="s-a-dynamic-admin-interface-it-s-not-just-scaffolding-it-s-the-whole-house">
<span id="a-dynamic-admin-interface-it-s-not-just-scaffolding-it-s-the-whole-house"></span><h2>A dynamic admin interface: it's not just scaffolding -- it's the whole house<a class="headerlink" href="#a-dynamic-admin-interface-it-s-not-just-scaffolding-it-s-the-whole-house" title="Permalink to this headline">¶</a></h2>
<p>Once your models are defined, Django can automatically create a professional,
production ready <a class="reference internal" href="../ref/contrib/admin/index.html"><em>administrative interface</em></a> -- a Web
site that lets authenticated users add, change and delete objects. It's as easy
as registering your model in the admin site:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># In models.py...</span>

<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Article</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">()</span>
    <span class="n">headline</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
    <span class="n">content</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
    <span class="n">reporter</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Reporter</span><span class="p">)</span>


<span class="c"># In admin.py in the same directory...</span>

<span class="kn">import</span> <span class="nn">models</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Article</span><span class="p">)</span>
</pre></div>
</div>
<p>The philosophy here is that your site is edited by a staff, or a client, or
maybe just you -- and you don't want to have to deal with creating backend
interfaces just to manage content.</p>
<p>One typical workflow in creating Django apps is to create models and get the
admin sites up and running as fast as possible, so your staff (or clients) can
start populating data. Then, develop the way data is presented to the public.</p>
</div>
<div class="section" id="s-design-your-urls">
<span id="design-your-urls"></span><h2>Design your URLs<a class="headerlink" href="#design-your-urls" title="Permalink to this headline">¶</a></h2>
<p>A clean, elegant URL scheme is an important detail in a high-quality Web
application. Django encourages beautiful URL design and doesn't put any cruft
in URLs, like <tt class="docutils literal"><span class="pre">.php</span></tt> or <tt class="docutils literal"><span class="pre">.asp</span></tt>.</p>
<p>To design URLs for an app, you create a Python module called a <a class="reference internal" href="../topics/http/urls.html"><em>URLconf</em></a>. A table of contents for your app, it contains a simple mapping
between URL patterns and Python callback functions. URLconfs also serve to
decouple URLs from Python code.</p>
<p>Here's what a URLconf might look like for the <tt class="docutils literal"><span class="pre">Reporter</span></tt>/<tt class="docutils literal"><span class="pre">Article</span></tt>
example above:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.year_archive&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/(\d{2})/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.month_archive&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r&#39;^articles/(\d{4})/(\d{2})/(\d+)/$&#39;</span><span class="p">,</span> <span class="s">&#39;mysite.views.article_detail&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The code above maps URLs, as simple regular expressions, to the location of
Python callback functions (&quot;views&quot;). The regular expressions use parenthesis to
&quot;capture&quot; values from the URLs. When a user requests a page, Django runs
through each pattern, in order, and stops at the first one that matches the
requested URL. (If none of them matches, Django calls a special-case 404 view.)
This is blazingly fast, because the regular expressions are compiled at load
time.</p>
<p>Once one of the regexes matches, Django imports and calls the given view, which
is a simple Python function. Each view gets passed a request object --
which contains request metadata -- and the values captured in the regex.</p>
<p>For example, if a user requested the URL &quot;/articles/2005/05/39323/&quot;, Django
would call the function <tt class="docutils literal"><span class="pre">mysite.views.article_detail(request,</span>
<span class="pre">'2005',</span> <span class="pre">'05',</span> <span class="pre">'39323')</span></tt>.</p>
</div>
<div class="section" id="s-write-your-views">
<span id="write-your-views"></span><h2>Write your views<a class="headerlink" href="#write-your-views" title="Permalink to this headline">¶</a></h2>
<p>Each view is responsible for doing one of two things: Returning an
<a class="reference internal" href="../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a> object containing the content for the
requested page, or raising an exception such as <tt class="xref py py-class docutils literal"><span class="pre">Http404</span></tt>.
The rest is up to you.</p>
<p>Generally, a view retrieves data according to the parameters, loads a template
and renders the template with the retrieved data. Here's an example view for
<tt class="docutils literal"><span class="pre">year_archive</span></tt> from above:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">year_archive</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">year</span><span class="p">):</span>
    <span class="n">a_list</span> <span class="o">=</span> <span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pub_date__year</span><span class="o">=</span><span class="n">year</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;news/year_archive.html&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;year&#39;</span><span class="p">:</span> <span class="n">year</span><span class="p">,</span> <span class="s">&#39;article_list&#39;</span><span class="p">:</span> <span class="n">a_list</span><span class="p">})</span>
</pre></div>
</div>
<p>This example uses Django's <a class="reference internal" href="../topics/templates.html"><em>template system</em></a>, which has
several powerful features but strives to stay simple enough for non-programmers
to use.</p>
</div>
<div class="section" id="s-design-your-templates">
<span id="design-your-templates"></span><h2>Design your templates<a class="headerlink" href="#design-your-templates" title="Permalink to this headline">¶</a></h2>
<p>The code above loads the <tt class="docutils literal"><span class="pre">news/year_archive.html</span></tt> template.</p>
<p>Django has a template search path, which allows you to minimize redundancy among
templates. In your Django settings, you specify a list of directories to check
for templates. If a template doesn't exist in the first directory, it checks the
second, and so on.</p>
<p>Let's say the <tt class="docutils literal"><span class="pre">news/article_detail.html</span></tt> template was found. Here's what that
might look like:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;base.html&quot;</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">block</span> <span class="nv">title</span> <span class="cp">%}</span>Articles for <span class="cp">{{</span> <span class="nv">year</span> <span class="cp">}}{%</span> <span class="k">endblock</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span>
<span class="nt">&lt;h1&gt;</span>Articles for <span class="cp">{{</span> <span class="nv">year</span> <span class="cp">}}</span><span class="nt">&lt;/h1&gt;</span>

<span class="cp">{%</span> <span class="k">for</span> <span class="nv">article</span> <span class="k">in</span> <span class="nv">article_list</span> <span class="cp">%}</span>
    <span class="nt">&lt;p&gt;</span><span class="cp">{{</span> <span class="nv">article.headline</span> <span class="cp">}}</span><span class="nt">&lt;/p&gt;</span>
    <span class="nt">&lt;p&gt;</span>By <span class="cp">{{</span> <span class="nv">article.reporter.full_name</span> <span class="cp">}}</span><span class="nt">&lt;/p&gt;</span>
    <span class="nt">&lt;p&gt;</span>Published <span class="cp">{{</span> <span class="nv">article.pub_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;F j, Y&quot;</span> <span class="cp">}}</span><span class="nt">&lt;/p&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Variables are surrounded by double-curly braces. <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">article.headline</span> <span class="pre">}}</span></tt>
means &quot;Output the value of the article's headline attribute.&quot; But dots aren't
used only for attribute lookup: They also can do dictionary-key lookup, index
lookup and function calls.</p>
<p>Note <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">article.pub_date|date:&quot;F</span> <span class="pre">j,</span> <span class="pre">Y&quot;</span> <span class="pre">}}</span></tt> uses a Unix-style &quot;pipe&quot; (the &quot;|&quot;
character). This is called a template filter, and it's a way to filter the value
of a variable. In this case, the date filter formats a Python datetime object in
the given format (as found in PHP's date function; yes, there is one good idea
in PHP).</p>
<p>You can chain together as many filters as you'd like. You can write custom
filters. You can write custom template tags, which run custom Python code behind
the scenes.</p>
<p>Finally, Django uses the concept of &quot;template inheritance&quot;: That's what the
<tt class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">&quot;base.html&quot;</span> <span class="pre">%}</span></tt> does. It means &quot;First load the template called
'base', which has defined a bunch of blocks, and fill the blocks with the
following blocks.&quot; In short, that lets you dramatically cut down on redundancy
in templates: each template has to define only what's unique to that template.</p>
<p>Here's what the &quot;base.html&quot; template might look like:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
    <span class="nt">&lt;title&gt;</span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">title</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
    <span class="nt">&lt;img</span> <span class="na">src=</span><span class="s">&quot;sitelogo.gif&quot;</span> <span class="na">alt=</span><span class="s">&quot;Logo&quot;</span> <span class="nt">/&gt;</span>
    <span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div>
<p>Simplistically, it defines the look-and-feel of the site (with the site's logo),
and provides &quot;holes&quot; for child templates to fill. This makes a site redesign as
easy as changing a single file -- the base template.</p>
<p>It also lets you create multiple versions of a site, with different base
templates, while reusing child templates. Django's creators have used this
technique to create strikingly different cell-phone editions of sites -- simply
by creating a new base template.</p>
<p>Note that you don't have to use Django's template system if you prefer another
system. While Django's template system is particularly well-integrated with
Django's model layer, nothing forces you to use it. For that matter, you don't
have to use Django's database API, either. You can use another database
abstraction layer, you can read XML files, you can read files off disk, or
anything you want. Each piece of Django -- models, views, templates -- is
decoupled from the next.</p>
</div>
<div class="section" id="s-this-is-just-the-surface">
<span id="this-is-just-the-surface"></span><h2>This is just the surface<a class="headerlink" href="#this-is-just-the-surface" title="Permalink to this headline">¶</a></h2>
<p>This has been only a quick overview of Django's functionality. Some more useful
features:</p>
<ul class="simple">
<li>A <a class="reference internal" href="../topics/cache.html"><em>caching framework</em></a> that integrates with memcached
or other backends.</li>
<li>A <a class="reference internal" href="../ref/contrib/syndication.html"><em>syndication framework</em></a> that makes
creating RSS and Atom feeds as easy as writing a small Python class.</li>
<li>More sexy automatically-generated admin features -- this overview barely
scratched the surface.</li>
</ul>
<p>The next obvious steps are for you to <a class="reference external" href="http://www.djangoproject.com/download/">download Django</a>, read <a class="reference internal" href="tutorial01.html"><em>the
tutorial</em></a> and join <a class="reference external" href="http://www.djangoproject.com/community/">the community</a>. Thanks for your
interest!</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="#">Django at a glance</a><ul>
<li><a class="reference internal" href="#design-your-model">Design your model</a></li>
<li><a class="reference internal" href="#install-it">Install it</a></li>
<li><a class="reference internal" href="#enjoy-the-free-api">Enjoy the free API</a></li>
<li><a class="reference internal" href="#a-dynamic-admin-interface-it-s-not-just-scaffolding-it-s-the-whole-house">A dynamic admin interface: it&#8217;s not just scaffolding &#8211; it&#8217;s the whole house</a></li>
<li><a class="reference internal" href="#design-your-urls">Design your URLs</a></li>
<li><a class="reference internal" href="#write-your-views">Write your views</a></li>
<li><a class="reference internal" href="#design-your-templates">Design your templates</a></li>
<li><a class="reference internal" href="#this-is-just-the-surface">This is just the surface</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="index.html">Getting started</a></li>
    
    
      <li>Next: <a href="install.html">Quick install guide</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">Getting started</a>
        
        <ul><li>Django at a glance</li></ul>
        </li></ul>
      </li>
  </ul>  

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/intro/overview.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="index.html" title="Getting started">previous</a> 
     |
    <a href="index.html" title="Getting started" accesskey="U">up</a>
   |
    <a href="install.html" title="Quick install guide">next</a> &raquo;</div>
    </div>
  </div>

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