2010-12-18 19:49:45 +00:00
2010-05-28 13:52:54 +00:00
< !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" / >
2012-07-11 11:05:47 +00:00
< title > Using categories in templates — Django Categories v1.0.5 documentation< / title >
2010-05-28 13:52:54 +00:00
< 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: '',
2012-06-04 12:46:22 +00:00
VERSION: '1.0.5',
2010-12-18 19:49:45 +00:00
COLLAPSE_INDEX: false,
2010-05-28 13:52:54 +00:00
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
< / script >
< script type = "text/javascript" src = "_static/jquery.js" > < / script >
2010-12-18 19:49:45 +00:00
< script type = "text/javascript" src = "_static/underscore.js" > < / script >
2010-05-28 13:52:54 +00:00
< script type = "text/javascript" src = "_static/doctools.js" > < / script >
2012-07-11 11:05:47 +00:00
< link rel = "top" title = "Django Categories v1.0.5 documentation" href = "index.html" / >
2010-12-18 19:49:45 +00:00
< link rel = "next" title = "Registering Models" href = "registering_models.html" / >
< link rel = "prev" title = "Getting Started" href = "getting_started.html" / >
2010-05-28 13:52:54 +00:00
< / head >
< body >
2010-05-28 14:38:57 +00:00
< div id = "docstitle" >
2012-07-11 11:05:47 +00:00
< p > Django Categories v1.0.5 documentation< / p >
2010-05-28 14:38:57 +00:00
< / div >
< div id = "header" >
2010-12-18 19:49:45 +00:00
< div id = "title" > < h1 > Using categories in templates< / h1 > < / div >
2010-05-28 14:38:57 +00:00
< 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 >
2010-12-18 19:49:45 +00:00
< 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 >
2010-05-28 14:38:57 +00:00
< / 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" >
2012-01-03 14:16:34 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "installation.html" > Installation< / a > < / li >
2010-12-18 19:49:45 +00:00
< 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 >
2011-09-04 23:57:58 +00:00
< 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 >
2010-12-18 19:49:45 +00:00
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "registering_models.html" > Registering Models< / a > < / li >
2011-08-01 13:48:50 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "adding_the_fields.html" > Adding the fields to the database< / a > < / li >
2012-02-08 16:23:58 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "custom_categories.html" > Creating Custom Categories< / a > < / li >
2011-05-16 12:58:37 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "reference/index.html" > Reference< / a > < / li >
2010-05-28 14:38:57 +00:00
< / ul >
< h3 > This Page< / h3 >
< ul class = "this-page-menu" >
< li > < a href = "_sources/usage.txt"
rel="nofollow">Show Source< / a > < / li >
2010-05-28 13:52:54 +00:00
< / ul >
2010-05-28 14:38:57 +00:00
< 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 >
2010-05-28 13:52:54 +00:00
< div class = "document" >
< div class = "documentwrapper" >
< div class = "bodywrapper" >
< div class = "body" >
2010-12-18 19:49:45 +00:00
< div class = "section" id = "using-categories-in-templates" >
2010-05-28 13:52:54 +00:00
< h1 > Using categories in templates< a class = "headerlink" href = "#using-categories-in-templates" title = "Permalink to this headline" > ¶< / a > < / h1 >
2011-09-04 23:57:58 +00:00
< 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 >
2012-02-08 16:23:58 +00:00
< 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 >
2011-09-04 23:57:58 +00:00
< 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 >
2010-05-28 13:52:54 +00:00
< 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 >
2012-07-11 11:05:47 +00:00
< dl class = "docutils" >
2010-05-28 13:52:54 +00:00
< dt > < tt class = "docutils literal" > < span class = "pre" > 'new_level'< / span > < / tt > < / dt >
2012-07-11 11:05:47 +00:00
< 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 >
2010-05-28 13:52:54 +00:00
< 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 >
2012-07-11 11:05:47 +00:00
< / blockquote >
2010-05-28 13:52:54 +00:00
< 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 >
2012-07-11 11:05:47 +00:00
< dl class = "docutils" >
2010-05-28 13:52:54 +00:00
< 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 >
2012-07-11 11:05:47 +00:00
< / blockquote >
2010-05-28 13:52:54 +00:00
< / div >
< / div >
< / div >
2010-05-28 14:38:57 +00:00
< / div >
2010-05-28 13:52:54 +00:00
< / div >
< / div >
< div class = "footer" >
2010-05-28 14:38:57 +00:00
< p >
2012-01-03 14:16:34 +00:00
© Copyright 2010-2012, Corey Oordt.
2012-07-11 11:05:47 +00:00
Created using < a href = "http://sphinx.pocoo.org/" > Sphinx< / a > 1.0.5.
2010-05-28 14:38:57 +00:00
< / p >
< / div >
< div class = "clearer" > < / div >
2010-05-28 13:52:54 +00:00
< / div >
2010-05-28 14:38:57 +00:00
< div id = "breadcrumbs" >
2010-12-18 19:49:45 +00:00
Using categories in templates
2010-05-28 14:38:57 +00:00
< / ul >
< / div >
< script type = "text/javascript" charset = "utf-8" src = "_static/toc.js" > < / script >
2010-05-28 13:52:54 +00:00
< / body >
< / html >