221 lines
13 KiB
HTML
221 lines
13 KiB
HTML
|
|
||
|
<!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>Using categories in templates — Django Categories v1.1 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.1',
|
||
|
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 Categories v1.1 documentation" href="index.html" />
|
||
|
<link rel="next" title="Registering Models" href="registering_models.html" />
|
||
|
<link rel="prev" title="Getting Started" href="getting_started.html" />
|
||
|
</head>
|
||
|
<body>
|
||
|
<div id="docstitle">
|
||
|
<p>Django Categories v1.1 documentation</p>
|
||
|
</div>
|
||
|
<div id="header">
|
||
|
<div id="title"><h1>Using categories in templates</h1></div>
|
||
|
<ul id="headerButtons">
|
||
|
<li id="toc_button"><div class="headerButton"><a href="#">Table of Contents</a></div></li>
|
||
|
<li id="page_buttons">
|
||
|
<div class="headerButton"><a href="genindex.html" title="General Index" accesskey="I">index</a></div>
|
||
|
<div class="headerButton"><a href="registering_models.html" title="Registering Models" accesskey="N">next</a></div>
|
||
|
<div class="headerButton"><a href="getting_started.html" title="Getting Started" accesskey="P">previous</a></div>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
|
||
|
<div id="sphinxsidebar">
|
||
|
<div class="sphinxsidebarwrapper">
|
||
|
<ul><li class="toctree-l1"><a href="index.html">Main Page</a></li></ul>
|
||
|
<ul class="current">
|
||
|
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installation</a></li>
|
||
|
<li class="toctree-l1"><a class="reference internal" href="getting_started.html">Getting Started</a></li>
|
||
|
<li class="toctree-l1 current"><a class="current reference internal" href="">Using categories in templates</a><ul>
|
||
|
<li class="toctree-l2"><a class="reference internal" href="#getting-all-items-within-a-category">Getting all items within a category</a></li>
|
||
|
<li class="toctree-l2"><a class="reference internal" href="#template-tags">Template Tags</a></li>
|
||
|
</ul>
|
||
|
</li>
|
||
|
<li class="toctree-l1"><a class="reference internal" href="registering_models.html">Registering Models</a></li>
|
||
|
<li class="toctree-l1"><a class="reference internal" href="adding_the_fields.html">Adding the fields to the database</a></li>
|
||
|
<li class="toctree-l1"><a class="reference internal" href="custom_categories.html">Creating Custom Categories</a></li>
|
||
|
<li class="toctree-l1"><a class="reference internal" href="reference/index.html">Reference</a></li>
|
||
|
</ul>
|
||
|
|
||
|
<h3>This Page</h3>
|
||
|
<ul class="this-page-menu">
|
||
|
<li><a href="_sources/usage.txt"
|
||
|
rel="nofollow">Show Source</a></li>
|
||
|
</ul>
|
||
|
<div id="searchbox" style="display: none">
|
||
|
|
||
|
<form class="search" action="search.html" method="get">
|
||
|
<div class="search-wrapper">
|
||
|
<span class="search-left"></span>
|
||
|
<input class="prettysearch" type="text" name="q" size="18" />
|
||
|
<span class="search-right"> </span>
|
||
|
</div>
|
||
|
<input type="submit" value="Search" class="searchbutton" />
|
||
|
<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>
|
||
|
|
||
|
|
||
|
|
||
|
<div class="document">
|
||
|
<div class="documentwrapper">
|
||
|
<div class="bodywrapper">
|
||
|
<div class="body">
|
||
|
|
||
|
<div class="section" id="using-categories-in-templates">
|
||
|
<h1>Using categories in templates<a class="headerlink" href="#using-categories-in-templates" title="Permalink to this headline">¶</a></h1>
|
||
|
<div class="section" id="getting-all-items-within-a-category">
|
||
|
<h2>Getting all items within a category<a class="headerlink" href="#getting-all-items-within-a-category" title="Permalink to this headline">¶</a></h2>
|
||
|
<p>The <a class="reference internal" href="reference/models.html#Category" title="Category"><tt class="xref py py-class docutils literal"><span class="pre">Category</span></tt></a> model automatically gets <a class="reference external" href="https://docs.djangoproject.com/en/1.3/topics/db/queries/#following-relationships-backward">reverse relationships</a> with all other models related to it.</p>
|
||
|
<p>This allows you access to the related objects from the template without altering any views. For example, if you only had <tt class="docutils literal"><span class="pre">Entry</span></tt> models related to <a class="reference internal" href="reference/models.html#Category" title="Category"><tt class="xref py py-class docutils literal"><span class="pre">Category</span></tt></a>, your <tt class="docutils literal"><span class="pre">categories/category_detail.html</span></tt> template could look like</p>
|
||
|
<div class="highlight-django"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
|
||
|
2
|
||
|
3
|
||
|
4
|
||
|
5
|
||
|
6
|
||
|
7
|
||
|
8
|
||
|
9
|
||
|
10
|
||
|
11
|
||
|
12
|
||
|
13
|
||
|
14
|
||
|
15
|
||
|
16
|
||
|
17
|
||
|
18
|
||
|
19
|
||
|
20
|
||
|
21
|
||
|
22
|
||
|
23
|
||
|
24
|
||
|
25
|
||
|
26
|
||
|
27
|
||
|
28</pre></div></td><td class="code"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">extends</span> <span class="s1">'categories/base.html'</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="x"><h1></span><span class="cp">{{</span> <span class="nv">category</span> <span class="cp">}}</span><span class="x"></h1></span>
|
||
|
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">category.parent</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="x"> <h2>Go up to </span>
|
||
|
<span class="x"> <a href="</span><span class="cp">{{</span> <span class="nv">category.parent.get_absolute_url</span> <span class="cp">}}</span><span class="x">"></span>
|
||
|
<span class="x"> </span><span class="cp">{{</span> <span class="nv">category.parent</span> <span class="cp">}}</span><span class="x"></span>
|
||
|
<span class="x"> </a></h2></span>
|
||
|
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">category.description</span> <span class="cp">%}</span><span class="x"><p></span><span class="cp">{{</span> <span class="nv">category.description</span> <span class="cp">}}</span><span class="x"></p></span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">category.children.count</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="x"> <h2>Subcategories</h2></span>
|
||
|
<span class="x"> <ul></span>
|
||
|
<span class="x"> </span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">child</span> <span class="k">in</span> <span class="nv">category.children.all</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="x"> <li><a href="</span><span class="cp">{{</span> <span class="nv">child.get_absolute_url</span> <span class="cp">}}</span><span class="x">"></span><span class="cp">{{</span> <span class="nv">child</span> <span class="cp">}}</span><span class="x"></a></li></span>
|
||
|
<span class="x"> </span><span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="x"> </ul></span>
|
||
|
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="x"><h2>Entries</h2></span>
|
||
|
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">category.entries_set.all</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="x"> </span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">entry</span> <span class="k">in</span> <span class="nv">category.entries_set.all</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="x"> <p><a href="</span><span class="cp">{{</span> <span class="nv">entry.get_absolute_url</span> <span class="cp">}}</span><span class="x">"></span><span class="cp">{{</span> <span class="nv">entry.headline</span> <span class="cp">}}</span><span class="x"></a></p></span>
|
||
|
<span class="x"> </span><span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
<span class="x"> <p><em>No entries for </span><span class="cp">{{</span> <span class="nv">category</span> <span class="cp">}}</span><span class="x"></em></p></span>
|
||
|
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
|
||
|
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="x"></span>
|
||
|
</pre></div>
|
||
|
</td></tr></table></div>
|
||
|
<p>If you have <tt class="docutils literal"><span class="pre">related_name</span></tt> parameters to the configuration (see <a class="reference internal" href="registering_models.html#registering-models"><em>Registering Models</em></a>), then you would use <tt class="docutils literal"><span class="pre">category.related_name.all</span></tt> instead of <tt class="docutils literal"><span class="pre">category.relatedmodel_set.all</span></tt>.</p>
|
||
|
</div>
|
||
|
<div class="section" id="template-tags">
|
||
|
<h2>Template Tags<a class="headerlink" href="#template-tags" title="Permalink to this headline">¶</a></h2>
|
||
|
<p>To use the template tags:</p>
|
||
|
<div class="highlight-python"><pre>{% import category_tags %}</pre>
|
||
|
</div>
|
||
|
<div class="section" id="tree-info">
|
||
|
<h3><tt class="docutils literal"><span class="pre">tree_info</span></tt><a class="headerlink" href="#tree-info" title="Permalink to this headline">¶</a></h3>
|
||
|
<p>Given a list of categories, iterates over the list, generating
|
||
|
two-tuples of the current tree item and a <tt class="docutils literal"><span class="pre">dict</span></tt> containing
|
||
|
information about the tree structure around the item, with the following
|
||
|
keys:</p>
|
||
|
<blockquote>
|
||
|
<div><dl class="docutils">
|
||
|
<dt><tt class="docutils literal"><span class="pre">'new_level'</span></tt></dt>
|
||
|
<dd><tt class="xref docutils literal"><span class="pre">True</span></tt> if the current item is the start of a new level in
|
||
|
the tree, <tt class="xref docutils literal"><span class="pre">False</span></tt> otherwise.</dd>
|
||
|
<dt><tt class="docutils literal"><span class="pre">'closed_levels'</span></tt></dt>
|
||
|
<dd>A list of levels which end after the current item. This will
|
||
|
be an empty list if the next item’s level is the same as or
|
||
|
greater than the level of the current item.</dd>
|
||
|
</dl>
|
||
|
</div></blockquote>
|
||
|
<p>An optional argument can be provided to specify extra details about the
|
||
|
structure which should appear in the <tt class="docutils literal"><span class="pre">dict</span></tt>. This should be a
|
||
|
comma-separated list of feature names. The valid feature names are:</p>
|
||
|
<blockquote>
|
||
|
<div><dl class="docutils">
|
||
|
<dt>ancestors</dt>
|
||
|
<dd><p class="first">Adds a list of unicode representations of the ancestors of the
|
||
|
current node, in descending order (root node first, immediate
|
||
|
parent last), under the key <tt class="docutils literal"><span class="pre">'ancestors'</span></tt>.</p>
|
||
|
<p>For example: given the sample tree below, the contents of the list
|
||
|
which would be available under the <tt class="docutils literal"><span class="pre">'ancestors'</span></tt> key are given
|
||
|
on the right:</p>
|
||
|
<div class="last highlight-python"><pre>Books -> []
|
||
|
Sci-fi -> [u'Books']
|
||
|
Dystopian Futures -> [u'Books', u'Sci-fi']</pre>
|
||
|
</div>
|
||
|
</dd>
|
||
|
</dl>
|
||
|
</div></blockquote>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="footer">
|
||
|
<p>
|
||
|
© Copyright 2010-2012, Corey Oordt.
|
||
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||
|
</p>
|
||
|
</div>
|
||
|
<div class="clearer"></div>
|
||
|
</div>
|
||
|
<div id="breadcrumbs">
|
||
|
Using categories in templates
|
||
|
</ul>
|
||
|
</div>
|
||
|
<script type="text/javascript" charset="utf-8" src="_static/toc.js"></script>
|
||
|
</body>
|
||
|
</html>
|