builtins.txt 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. ==================================
  2. Built-in template tags and filters
  3. ==================================
  4. This document describes Django's built-in template tags and filters. It is
  5. recommended that you use the :doc:`automatic documentation
  6. </ref/contrib/admin/admindocs>`, if available, as this will also include
  7. documentation for any custom tags or filters installed.
  8. .. _ref-templates-builtins-tags:
  9. Built-in tag reference
  10. ----------------------
  11. .. highlightlang:: html+django
  12. .. templatetag:: autoescape
  13. autoescape
  14. ~~~~~~~~~~
  15. Control the current auto-escaping behavior. This tag takes either ``on`` or
  16. ``off`` as an argument and that determines whether auto-escaping is in effect
  17. inside the block. The block is closed with an ``endautoescape`` ending tag.
  18. When auto-escaping is in effect, all variable content has HTML escaping applied
  19. to it before placing the result into the output (but after any filters have
  20. been applied). This is equivalent to manually applying the ``escape`` filter
  21. to each variable.
  22. The only exceptions are variables that are already marked as "safe" from
  23. escaping, either by the code that populated the variable, or because it has had
  24. the ``safe`` or ``escape`` filters applied.
  25. Sample usage::
  26. {% autoescape on %}
  27. {{ body }}
  28. {% endautoescape %}
  29. .. templatetag:: block
  30. block
  31. ~~~~~
  32. Define a block that can be overridden by child templates. See
  33. :ref:`Template inheritance <template-inheritance>` for more information.
  34. .. templatetag:: comment
  35. comment
  36. ~~~~~~~
  37. Ignore everything between ``{% comment %}`` and ``{% endcomment %}``
  38. .. templatetag:: csrf_token
  39. csrf_token
  40. ~~~~~~~~~~
  41. In the Django 1.1.X series, this is a no-op tag that returns an empty string for
  42. future compatibility purposes. In Django 1.2 and later, it is used for CSRF
  43. protection, as described in the documentation for :doc:`Cross Site Request
  44. Forgeries </ref/contrib/csrf>`.
  45. .. templatetag:: cycle
  46. cycle
  47. ~~~~~
  48. Cycle among the given strings or variables each time this tag is encountered.
  49. Within a loop, cycles among the given strings each time through the
  50. loop::
  51. {% for o in some_list %}
  52. <tr class="{% cycle 'row1' 'row2' %}">
  53. ...
  54. </tr>
  55. {% endfor %}
  56. You can use variables, too. For example, if you have two template variables,
  57. ``rowvalue1`` and ``rowvalue2``, you can cycle between their values like this::
  58. {% for o in some_list %}
  59. <tr class="{% cycle rowvalue1 rowvalue2 %}">
  60. ...
  61. </tr>
  62. {% endfor %}
  63. Yes, you can mix variables and strings::
  64. {% for o in some_list %}
  65. <tr class="{% cycle 'row1' rowvalue2 'row3' %}">
  66. ...
  67. </tr>
  68. {% endfor %}
  69. In some cases you might want to refer to the next value of a cycle from
  70. outside of a loop. To do this, just give the ``{% cycle %}`` tag a name, using
  71. "as", like this::
  72. {% cycle 'row1' 'row2' as rowcolors %}
  73. From then on, you can insert the current value of the cycle wherever you'd like
  74. in your template::
  75. <tr class="{% cycle rowcolors %}">...</tr>
  76. <tr class="{% cycle rowcolors %}">...</tr>
  77. You can use any number of values in a ``{% cycle %}`` tag, separated by spaces.
  78. Values enclosed in single (``'``) or double quotes (``"``) are treated as
  79. string literals, while values without quotes are treated as template variables.
  80. Note that the variables included in the cycle will not be escaped.
  81. This is because template tags do not escape their content. Any HTML or
  82. Javascript code contained in the printed variable will be rendered
  83. as-is, which could potentially lead to security issues.
  84. If you need to escape the variables in the cycle, you must do so
  85. explicitly::
  86. {% filter force_escape %}
  87. {% cycle var1 var2 var3 %}
  88. {% endfilter %}
  89. For backwards compatibility, the ``{% cycle %}`` tag supports the much inferior
  90. old syntax from previous Django versions. You shouldn't use this in any new
  91. projects, but for the sake of the people who are still using it, here's what it
  92. looks like::
  93. {% cycle row1,row2,row3 %}
  94. In this syntax, each value gets interpreted as a literal string, and there's no
  95. way to specify variable values. Or literal commas. Or spaces. Did we mention
  96. you shouldn't use this syntax in any new projects?
  97. .. versionadded:: 1.3
  98. By default, when you use the ``as`` keyword with the cycle tag, the
  99. usage of ``{% cycle %}`` that declares the cycle will itself output
  100. the first value in the cycle. This could be a problem if you want to
  101. use the value in a nested loop or an included template. If you want to
  102. just declare the cycle, but not output the first value, you can add a
  103. ``silent`` keyword as the last keyword in the tag. For example::
  104. {% cycle 'row1' 'row2' as rowcolors silent %}
  105. {% for obj in some_list %}
  106. <tr class="{% cycle rowcolors %}">{{ obj }}</tr>
  107. {% endfor %}
  108. This will output a list of ``<tr>`` elements with ``class``
  109. alternating between ``row1`` and ``row2``. If the ``silent`` keyword
  110. were to be omitted, ``row1`` would be emitted as normal text, outside
  111. the list of ``<tr>`` elements, and the first ``<tr>`` would have a
  112. class of ``row2``.
  113. .. templatetag:: debug
  114. debug
  115. ~~~~~
  116. Output a whole load of debugging information, including the current context and
  117. imported modules.
  118. .. templatetag:: extends
  119. extends
  120. ~~~~~~~
  121. Signal that this template extends a parent template.
  122. This tag can be used in two ways:
  123. * ``{% extends "base.html" %}`` (with quotes) uses the literal value
  124. ``"base.html"`` as the name of the parent template to extend.
  125. * ``{% extends variable %}`` uses the value of ``variable``. If the variable
  126. evaluates to a string, Django will use that string as the name of the
  127. parent template. If the variable evaluates to a ``Template`` object,
  128. Django will use that object as the parent template.
  129. See :ref:`template-inheritance` for more information.
  130. .. templatetag:: filter
  131. filter
  132. ~~~~~~
  133. Filter the contents of the variable through variable filters.
  134. Filters can also be piped through each other, and they can have arguments --
  135. just like in variable syntax.
  136. Sample usage::
  137. {% filter force_escape|lower %}
  138. This text will be HTML-escaped, and will appear in all lowercase.
  139. {% endfilter %}
  140. .. templatetag:: firstof
  141. firstof
  142. ~~~~~~~
  143. Outputs the first variable passed that is not False, without escaping.
  144. Outputs nothing if all the passed variables are False.
  145. Sample usage::
  146. {% firstof var1 var2 var3 %}
  147. This is equivalent to::
  148. {% if var1 %}
  149. {{ var1|safe }}
  150. {% else %}{% if var2 %}
  151. {{ var2|safe }}
  152. {% else %}{% if var3 %}
  153. {{ var3|safe }}
  154. {% endif %}{% endif %}{% endif %}
  155. You can also use a literal string as a fallback value in case all
  156. passed variables are False::
  157. {% firstof var1 var2 var3 "fallback value" %}
  158. Note that the variables included in the firstof tag will not be
  159. escaped. This is because template tags do not escape their content.
  160. Any HTML or Javascript code contained in the printed variable will be
  161. rendered as-is, which could potentially lead to security issues.
  162. If you need to escape the variables in the firstof tag, you must do so
  163. explicitly::
  164. {% filter force_escape %}
  165. {% firstof var1 var2 var3 "fallback value" %}
  166. {% endfilter %}
  167. .. templatetag:: for
  168. for
  169. ~~~
  170. Loop over each item in an array. For example, to display a list of athletes
  171. provided in ``athlete_list``::
  172. <ul>
  173. {% for athlete in athlete_list %}
  174. <li>{{ athlete.name }}</li>
  175. {% endfor %}
  176. </ul>
  177. You can loop over a list in reverse by using ``{% for obj in list reversed %}``.
  178. If you need to loop over a list of lists, you can unpack the values
  179. in each sub-list into individual variables. For example, if your context
  180. contains a list of (x,y) coordinates called ``points``, you could use the
  181. following to output the list of points::
  182. {% for x, y in points %}
  183. There is a point at {{ x }},{{ y }}
  184. {% endfor %}
  185. This can also be useful if you need to access the items in a dictionary.
  186. For example, if your context contained a dictionary ``data``, the following
  187. would display the keys and values of the dictionary::
  188. {% for key, value in data.items %}
  189. {{ key }}: {{ value }}
  190. {% endfor %}
  191. The for loop sets a number of variables available within the loop:
  192. ========================== ================================================
  193. Variable Description
  194. ========================== ================================================
  195. ``forloop.counter`` The current iteration of the loop (1-indexed)
  196. ``forloop.counter0`` The current iteration of the loop (0-indexed)
  197. ``forloop.revcounter`` The number of iterations from the end of the
  198. loop (1-indexed)
  199. ``forloop.revcounter0`` The number of iterations from the end of the
  200. loop (0-indexed)
  201. ``forloop.first`` True if this is the first time through the loop
  202. ``forloop.last`` True if this is the last time through the loop
  203. ``forloop.parentloop`` For nested loops, this is the loop "above" the
  204. current one
  205. ========================== ================================================
  206. for ... empty
  207. ^^^^^^^^^^^^^
  208. The ``for`` tag can take an optional ``{% empty %}`` clause that will be
  209. displayed if the given array is empty or could not be found::
  210. <ul>
  211. {% for athlete in athlete_list %}
  212. <li>{{ athlete.name }}</li>
  213. {% empty %}
  214. <li>Sorry, no athlete in this list!</li>
  215. {% endfor %}
  216. <ul>
  217. The above is equivalent to -- but shorter, cleaner, and possibly faster
  218. than -- the following::
  219. <ul>
  220. {% if athlete_list %}
  221. {% for athlete in athlete_list %}
  222. <li>{{ athlete.name }}</li>
  223. {% endfor %}
  224. {% else %}
  225. <li>Sorry, no athletes in this list.</li>
  226. {% endif %}
  227. </ul>
  228. .. templatetag:: if
  229. if
  230. ~~
  231. The ``{% if %}`` tag evaluates a variable, and if that variable is "true" (i.e.
  232. exists, is not empty, and is not a false boolean value) the contents of the
  233. block are output::
  234. {% if athlete_list %}
  235. Number of athletes: {{ athlete_list|length }}
  236. {% else %}
  237. No athletes.
  238. {% endif %}
  239. In the above, if ``athlete_list`` is not empty, the number of athletes will be
  240. displayed by the ``{{ athlete_list|length }}`` variable.
  241. As you can see, the ``if`` tag can take an optional ``{% else %}`` clause that
  242. will be displayed if the test fails.
  243. Boolean operators
  244. ^^^^^^^^^^^^^^^^^
  245. ``if`` tags may use ``and``, ``or`` or ``not`` to test a number of variables or
  246. to negate a given variable::
  247. {% if athlete_list and coach_list %}
  248. Both athletes and coaches are available.
  249. {% endif %}
  250. {% if not athlete_list %}
  251. There are no athletes.
  252. {% endif %}
  253. {% if athlete_list or coach_list %}
  254. There are some athletes or some coaches.
  255. {% endif %}
  256. {% if not athlete_list or coach_list %}
  257. There are no athletes or there are some coaches (OK, so
  258. writing English translations of boolean logic sounds
  259. stupid; it's not our fault).
  260. {% endif %}
  261. {% if athlete_list and not coach_list %}
  262. There are some athletes and absolutely no coaches.
  263. {% endif %}
  264. .. versionchanged:: 1.2
  265. Use of both ``and`` and ``or`` clauses within the same tag is allowed, with
  266. ``and`` having higher precedence than ``or`` e.g.::
  267. {% if athlete_list and coach_list or cheerleader_list %}
  268. will be interpreted like:
  269. .. code-block:: python
  270. if (athlete_list and coach_list) or cheerleader_list
  271. Use of actual brackets in the ``if`` tag is invalid syntax. If you need them to
  272. indicate precedence, you should use nested ``if`` tags.
  273. .. versionadded:: 1.2
  274. ``if`` tags may also use the operators ``==``, ``!=``, ``<``, ``>``,
  275. ``<=``, ``>=`` and ``in`` which work as follows:
  276. ``==`` operator
  277. ^^^^^^^^^^^^^^^
  278. Equality. Example::
  279. {% if somevar == "x" %}
  280. This appears if variable somevar equals the string "x"
  281. {% endif %}
  282. ``!=`` operator
  283. ^^^^^^^^^^^^^^^
  284. Inequality. Example::
  285. {% if somevar != "x" %}
  286. This appears if variable somevar does not equal the string "x",
  287. or if somevar is not found in the context
  288. {% endif %}
  289. ``<`` operator
  290. ^^^^^^^^^^^^^^
  291. Less than. Example::
  292. {% if somevar < 100 %}
  293. This appears if variable somevar is less than 100.
  294. {% endif %}
  295. ``>`` operator
  296. ^^^^^^^^^^^^^^
  297. Greater than. Example::
  298. {% if somevar > 0 %}
  299. This appears if variable somevar is greater than 0.
  300. {% endif %}
  301. ``<=`` operator
  302. ^^^^^^^^^^^^^^^
  303. Less than or equal to. Example::
  304. {% if somevar <= 100 %}
  305. This appears if variable somevar is less than 100 or equal to 100.
  306. {% endif %}
  307. ``>=`` operator
  308. ^^^^^^^^^^^^^^^
  309. Greater than or equal to. Example::
  310. {% if somevar >= 1 %}
  311. This appears if variable somevar is greater than 1 or equal to 1.
  312. {% endif %}
  313. ``in`` operator
  314. ^^^^^^^^^^^^^^^
  315. Contained within. This operator is supported by many Python containers to test
  316. whether the given value is in the container. The following are some examples of
  317. how ``x in y`` will be interpreted::
  318. {% if "bc" in "abcdef" %}
  319. This appears since "bc" is a substring of "abcdef"
  320. {% endif %}
  321. {% if "hello" in greetings %}
  322. If greetings is a list or set, one element of which is the string
  323. "hello", this will appear.
  324. {% endif %}
  325. {% if user in users %}
  326. If users is a QuerySet, this will appear if user is an
  327. instance that belongs to the QuerySet.
  328. {% endif %}
  329. ``not in`` operator
  330. ~~~~~~~~~~~~~~~~~~~~
  331. Not contained within. This is the negation of the ``in`` operator.
  332. The comparison operators cannot be 'chained' like in Python or in mathematical
  333. notation. For example, instead of using::
  334. {% if a > b > c %} (WRONG)
  335. you should use::
  336. {% if a > b and b > c %}
  337. Filters
  338. ^^^^^^^
  339. You can also use filters in the ``if`` expression. For example::
  340. {% if messages|length >= 100 %}
  341. You have lots of messages today!
  342. {% endif %}
  343. Complex expressions
  344. ^^^^^^^^^^^^^^^^^^^
  345. All of the above can be combined to form complex expressions. For such
  346. expressions, it can be important to know how the operators are grouped when the
  347. expression is evaluated - that is, the precedence rules. The precedence of the
  348. operators, from lowest to highest, is as follows:
  349. * ``or``
  350. * ``and``
  351. * ``not``
  352. * ``in``
  353. * ``==``, ``!=``, ``<``, ``>``,``<=``, ``>=``
  354. (This follows Python exactly). So, for example, the following complex if tag:
  355. {% if a == b or c == d and e %}
  356. ...will be interpreted as:
  357. .. code-block:: python
  358. (a == b) or ((c == d) and e)
  359. If you need different precedence, you will need to use nested if tags. Sometimes
  360. that is better for clarity anyway, for the sake of those who do not know the
  361. precedence rules.
  362. .. templatetag:: ifchanged
  363. ifchanged
  364. ~~~~~~~~~
  365. Check if a value has changed from the last iteration of a loop.
  366. The 'ifchanged' block tag is used within a loop. It has two possible uses.
  367. 1. Checks its own rendered contents against its previous state and only
  368. displays the content if it has changed. For example, this displays a list of
  369. days, only displaying the month if it changes::
  370. <h1>Archive for {{ year }}</h1>
  371. {% for date in days %}
  372. {% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}
  373. <a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a>
  374. {% endfor %}
  375. 2. If given a variable, check whether that variable has changed. For
  376. example, the following shows the date every time it changes, but
  377. only shows the hour if both the hour and the date has changed::
  378. {% for date in days %}
  379. {% ifchanged date.date %} {{ date.date }} {% endifchanged %}
  380. {% ifchanged date.hour date.date %}
  381. {{ date.hour }}
  382. {% endifchanged %}
  383. {% endfor %}
  384. The ``ifchanged`` tag can also take an optional ``{% else %}`` clause that
  385. will be displayed if the value has not changed::
  386. {% for match in matches %}
  387. <div style="background-color:
  388. {% ifchanged match.ballot_id %}
  389. {% cycle "red" "blue" %}
  390. {% else %}
  391. grey
  392. {% endifchanged %}
  393. ">{{ match }}</div>
  394. {% endfor %}
  395. .. templatetag:: ifequal
  396. ifequal
  397. ~~~~~~~
  398. Output the contents of the block if the two arguments equal each other.
  399. Example::
  400. {% ifequal user.id comment.user_id %}
  401. ...
  402. {% endifequal %}
  403. As in the ``{% if %}`` tag, an ``{% else %}`` clause is optional.
  404. The arguments can be hard-coded strings, so the following is valid::
  405. {% ifequal user.username "adrian" %}
  406. ...
  407. {% endifequal %}
  408. It is only possible to compare an argument to template variables or strings.
  409. You cannot check for equality with Python objects such as ``True`` or
  410. ``False``. If you need to test if something is true or false, use the ``if``
  411. tag instead.
  412. .. versionadded:: 1.2
  413. An alternative to the ``ifequal`` tag is to use the :ttag:`if` tag and the ``==`` operator.
  414. .. templatetag:: ifnotequal
  415. ifnotequal
  416. ~~~~~~~~~~
  417. Just like ``ifequal``, except it tests that the two arguments are not equal.
  418. .. versionadded:: 1.2
  419. An alternative to the ``ifnotequal`` tag is to use the :ttag:`if` tag and the ``!=`` operator.
  420. .. templatetag:: include
  421. include
  422. ~~~~~~~
  423. Loads a template and renders it with the current context. This is a way of
  424. "including" other templates within a template.
  425. The template name can either be a variable or a hard-coded (quoted) string,
  426. in either single or double quotes.
  427. This example includes the contents of the template ``"foo/bar.html"``::
  428. {% include "foo/bar.html" %}
  429. This example includes the contents of the template whose name is contained in
  430. the variable ``template_name``::
  431. {% include template_name %}
  432. An included template is rendered with the context of the template that's
  433. including it. This example produces the output ``"Hello, John"``:
  434. * Context: variable ``person`` is set to ``"john"``.
  435. * Template::
  436. {% include "name_snippet.html" %}
  437. * The ``name_snippet.html`` template::
  438. {{ greeting }}, {{ person|default:"friend" }}!
  439. .. versionchanged:: 1.3
  440. Additional context and exclusive context.
  441. You can pass additional context to the template using keyword arguments::
  442. {% include "name_snippet.html" with person="Jane" greeting="Hello" "%}
  443. If you want to only render the context with the variables provided (or even
  444. no variables at all), use the ``only`` option::
  445. {% include "name_snippet.html" with greeting="Hi" only %}
  446. .. note::
  447. The :ttag:`include` tag should be considered as an implementation of
  448. "render this subtemplate and include the HTML", not as "parse this
  449. subtemplate and include its contents as if it were part of the parent".
  450. This means that there is no shared state between included templates --
  451. each include is a completely independent rendering process.
  452. See also: ``{% ssi %}``.
  453. .. templatetag:: load
  454. load
  455. ~~~~
  456. Load a custom template tag set.
  457. For example, the following template would load all the tags and filters
  458. registered in ``somelibrary`` and ``otherlibrary``::
  459. {% load somelibrary otherlibrary %}
  460. You can also selectively load individual filters or tags from a library, using
  461. the ``from`` argument. In this example, the template tags/filters named ``foo``
  462. and ``bar`` will be loaded from ``somelibrary``::
  463. {% load foo bar from somelibrary %}
  464. See :doc:`Custom tag and filter libraries </howto/custom-template-tags>` for
  465. more information.
  466. .. templatetag:: now
  467. now
  468. ~~~
  469. Display the current date and/or time, using a format according to the given
  470. string. Such string can contain format specifiers characters as described
  471. in the :tfilter:`date` filter section.
  472. Example::
  473. It is {% now "jS F Y H:i" %}
  474. Note that you can backslash-escape a format string if you want to use the
  475. "raw" value. In this example, "f" is backslash-escaped, because otherwise
  476. "f" is a format string that displays the time. The "o" doesn't need to be
  477. escaped, because it's not a format character::
  478. It is the {% now "jS o\f F" %}
  479. This would display as "It is the 4th of September".
  480. .. templatetag:: regroup
  481. regroup
  482. ~~~~~~~
  483. Regroup a list of alike objects by a common attribute.
  484. This complex tag is best illustrated by use of an example: say that ``people``
  485. is a list of people represented by dictionaries with ``first_name``,
  486. ``last_name``, and ``gender`` keys:
  487. .. code-block:: python
  488. people = [
  489. {'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'},
  490. {'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'},
  491. {'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'},
  492. {'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'},
  493. {'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'},
  494. ]
  495. ...and you'd like to display a hierarchical list that is ordered by gender,
  496. like this:
  497. * Male:
  498. * George Bush
  499. * Bill Clinton
  500. * Female:
  501. * Margaret Thatcher
  502. * Condoleezza Rice
  503. * Unknown:
  504. * Pat Smith
  505. You can use the ``{% regroup %}`` tag to group the list of people by gender.
  506. The following snippet of template code would accomplish this::
  507. {% regroup people by gender as gender_list %}
  508. <ul>
  509. {% for gender in gender_list %}
  510. <li>{{ gender.grouper }}
  511. <ul>
  512. {% for item in gender.list %}
  513. <li>{{ item.first_name }} {{ item.last_name }}</li>
  514. {% endfor %}
  515. </ul>
  516. </li>
  517. {% endfor %}
  518. </ul>
  519. Let's walk through this example. ``{% regroup %}`` takes three arguments: the
  520. list you want to regroup, the attribute to group by, and the name of the
  521. resulting list. Here, we're regrouping the ``people`` list by the ``gender``
  522. attribute and calling the result ``gender_list``.
  523. ``{% regroup %}`` produces a list (in this case, ``gender_list``) of
  524. **group objects**. Each group object has two attributes:
  525. * ``grouper`` -- the item that was grouped by (e.g., the string "Male" or
  526. "Female").
  527. * ``list`` -- a list of all items in this group (e.g., a list of all people
  528. with gender='Male').
  529. Note that ``{% regroup %}`` does not order its input! Our example relies on
  530. the fact that the ``people`` list was ordered by ``gender`` in the first place.
  531. If the ``people`` list did *not* order its members by ``gender``, the regrouping
  532. would naively display more than one group for a single gender. For example,
  533. say the ``people`` list was set to this (note that the males are not grouped
  534. together):
  535. .. code-block:: python
  536. people = [
  537. {'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'},
  538. {'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'},
  539. {'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'},
  540. {'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'},
  541. {'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'},
  542. ]
  543. With this input for ``people``, the example ``{% regroup %}`` template code
  544. above would result in the following output:
  545. * Male:
  546. * Bill Clinton
  547. * Unknown:
  548. * Pat Smith
  549. * Female:
  550. * Margaret Thatcher
  551. * Male:
  552. * George Bush
  553. * Female:
  554. * Condoleezza Rice
  555. The easiest solution to this gotcha is to make sure in your view code that the
  556. data is ordered according to how you want to display it.
  557. Another solution is to sort the data in the template using the ``dictsort``
  558. filter, if your data is in a list of dictionaries::
  559. {% regroup people|dictsort:"gender" by gender as gender_list %}
  560. Grouping on other properties
  561. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  562. Any valid template lookup is a legal grouping attribute for the regroup
  563. tag, including methods, attributes, dictionary keys and list items. For
  564. example, if the "gender" field is a foreign key to a class with
  565. an attribute "description," you could use::
  566. {% regroup people by gender.description as gender_list %}
  567. Or, if ``gender`` is a field with ``choices``, it will have a
  568. :meth:`~django.db.models.Model.get_FOO_display` method available as an
  569. attribute, allowing you to group on the display string rather than the
  570. ``choices`` key::
  571. {% regroup people by get_gender_display as gender_list %}
  572. ``{{ gender.grouper }}`` will now display the value fields from the
  573. ``choices`` set rather than the keys.
  574. .. templatetag:: spaceless
  575. spaceless
  576. ~~~~~~~~~
  577. Removes whitespace between HTML tags. This includes tab
  578. characters and newlines.
  579. Example usage::
  580. {% spaceless %}
  581. <p>
  582. <a href="foo/">Foo</a>
  583. </p>
  584. {% endspaceless %}
  585. This example would return this HTML::
  586. <p><a href="foo/">Foo</a></p>
  587. Only space between *tags* is removed -- not space between tags and text. In
  588. this example, the space around ``Hello`` won't be stripped::
  589. {% spaceless %}
  590. <strong>
  591. Hello
  592. </strong>
  593. {% endspaceless %}
  594. .. templatetag:: ssi
  595. ssi
  596. ~~~
  597. Output the contents of a given file into the page.
  598. Like a simple "include" tag, ``{% ssi %}`` includes the contents of another
  599. file -- which must be specified using an absolute path -- in the current
  600. page::
  601. {% ssi /home/html/ljworld.com/includes/right_generic.html %}
  602. If the optional "parsed" parameter is given, the contents of the included
  603. file are evaluated as template code, within the current context::
  604. {% ssi /home/html/ljworld.com/includes/right_generic.html parsed %}
  605. Note that if you use ``{% ssi %}``, you'll need to define
  606. :setting:`ALLOWED_INCLUDE_ROOTS` in your Django settings, as a security measure.
  607. See also: ``{% include %}``.
  608. .. admonition:: Forwards compatibility
  609. .. versionchanged:: 1.3
  610. In Django 1.5, the behavior of the :ttag:`ssi` template tag will
  611. change, with the first argument being made into a context
  612. variable, rather than being a special case unquoted constant. This
  613. will allow the :ttag:`ssi` tag to use a context variable as the
  614. value of the page to be included.
  615. In order to provide a forwards compatibility path, Django 1.3
  616. provides a future compatibility library -- ``future`` -- that
  617. implements the new behavior. To use this library, add a
  618. :ttag:`load` call at the top of any template using the :ttag:`ssi`
  619. tag, and wrap the first argument to the :ttag:`ssi` tag in quotes.
  620. For example::
  621. {% load ssi from future %}
  622. {% ssi '/home/html/ljworld.com/includes/right_generic.html' %}
  623. In Django 1.5, the unquoted constant behavior will be replaced
  624. with the behavior provided by the ``future`` tag library.
  625. Existing templates should be migrated to use the new syntax.
  626. .. templatetag:: templatetag
  627. templatetag
  628. ~~~~~~~~~~~
  629. Output one of the syntax characters used to compose template tags.
  630. Since the template system has no concept of "escaping", to display one of the
  631. bits used in template tags, you must use the ``{% templatetag %}`` tag.
  632. The argument tells which template bit to output:
  633. ================== =======
  634. Argument Outputs
  635. ================== =======
  636. ``openblock`` ``{%``
  637. ``closeblock`` ``%}``
  638. ``openvariable`` ``{{``
  639. ``closevariable`` ``}}``
  640. ``openbrace`` ``{``
  641. ``closebrace`` ``}``
  642. ``opencomment`` ``{#``
  643. ``closecomment`` ``#}``
  644. ================== =======
  645. .. templatetag:: url
  646. url
  647. ~~~
  648. Returns an absolute path reference (a URL without the domain name) matching a
  649. given view function and optional parameters. This is a way to output links
  650. without violating the DRY principle by having to hard-code URLs in your
  651. templates::
  652. {% url path.to.some_view v1 v2 %}
  653. The first argument is a path to a view function in the format
  654. ``package.package.module.function``. Additional arguments are optional and
  655. should be space-separated values that will be used as arguments in the URL.
  656. The example above shows passing positional arguments. Alternatively you may
  657. use keyword syntax::
  658. {% url path.to.some_view arg1=v1 arg2=v2 %}
  659. Do not mix both positional and keyword syntax in a single call. All arguments
  660. required by the URLconf should be present.
  661. For example, suppose you have a view, ``app_views.client``, whose URLconf
  662. takes a client ID (here, ``client()`` is a method inside the views file
  663. ``app_views.py``). The URLconf line might look like this:
  664. .. code-block:: python
  665. ('^client/(\d+)/$', 'app_views.client')
  666. If this app's URLconf is included into the project's URLconf under a path
  667. such as this:
  668. .. code-block:: python
  669. ('^clients/', include('project_name.app_name.urls'))
  670. ...then, in a template, you can create a link to this view like this::
  671. {% url app_views.client client.id %}
  672. The template tag will output the string ``/clients/client/123/``.
  673. If you're using :ref:`named URL patterns <naming-url-patterns>`, you can
  674. refer to the name of the pattern in the ``url`` tag instead of using the
  675. path to the view.
  676. Note that if the URL you're reversing doesn't exist, you'll get an
  677. :exc:`~django.core.urlresolvers.NoReverseMatch` exception raised, which will
  678. cause your site to display an error page.
  679. If you'd like to retrieve a URL without displaying it, you can use a slightly
  680. different call::
  681. {% url path.to.view arg arg2 as the_url %}
  682. <a href="{{ the_url }}">I'm linking to {{ the_url }}</a>
  683. This ``{% url ... as var %}`` syntax will *not* cause an error if the view is
  684. missing. In practice you'll use this to link to views that are optional::
  685. {% url path.to.view as the_url %}
  686. {% if the_url %}
  687. <a href="{{ the_url }}">Link to optional stuff</a>
  688. {% endif %}
  689. If you'd like to retrieve a namespaced URL, specify the fully qualified name::
  690. {% url myapp:view-name %}
  691. This will follow the normal :ref:`namespaced URL resolution strategy
  692. <topics-http-reversing-url-namespaces>`, including using any hints provided
  693. by the context as to the current application.
  694. .. versionchanged:: 1.2
  695. For backwards compatibility, the ``{% url %}`` tag also supports the
  696. use of commas to separate arguments. You shouldn't use this in any new
  697. projects, but for the sake of the people who are still using it,
  698. here's what it looks like::
  699. {% url path.to.view arg,arg2 %}
  700. {% url path.to.view arg, arg2 %}
  701. This syntax doesn't support the use of literal commas, or equals
  702. signs. Did we mention you shouldn't use this syntax in any new
  703. projects?
  704. .. admonition:: Forwards compatibility
  705. .. versionchanged:: 1.3
  706. In Django 1.5, the behavior of the :ttag:`url` template tag will
  707. change, with the first argument being made into a context
  708. variable, rather than being a special case unquoted constant. This
  709. will allow the :ttag:`url` tag to use a context variable as the
  710. value of the URL name to be reversed.
  711. In order to provide a forwards compatibility path, Django 1.3
  712. provides a future compatibility library -- ``future`` -- that
  713. implements the new behavior. To use this library, add a
  714. :ttag:`load` call at the top of any template using the :ttag:`url`
  715. tag, and wrap the first argument to the :ttag:`url` tag in quotes.
  716. For example::
  717. {% load url from future %}
  718. {% url 'myapp:view-name' %}
  719. The new library also drops support for the comma syntax for
  720. separating arguments to the :ttag:`url` template tag.
  721. In Django 1.5, the old behavior will be replaced with the behavior
  722. provided by the ``future`` tag library. Existing templates be
  723. migrated to use the new syntax.
  724. .. templatetag:: widthratio
  725. widthratio
  726. ~~~~~~~~~~
  727. For creating bar charts and such, this tag calculates the ratio of a given value
  728. to a maximum value, and then applies that ratio to a constant.
  729. For example::
  730. <img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" />
  731. Above, if ``this_value`` is 175 and ``max_value`` is 200, the image in the
  732. above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5
  733. which is rounded up to 88).
  734. .. templatetag:: with
  735. with
  736. ~~~~
  737. .. versionchanged:: 1.3
  738. New keyword argument format and multiple variable assignments.
  739. Caches a complex variable under a simpler name. This is useful when accessing
  740. an "expensive" method (e.g., one that hits the database) multiple times.
  741. For example::
  742. {% with total=business.employees.count %}
  743. {{ total }} employee{{ total|pluralize }}
  744. {% endwith %}
  745. The populated variable (in the example above, ``total``) is only available
  746. between the ``{% with %}`` and ``{% endwith %}`` tags.
  747. You can assign more than one context variable::
  748. {% with alpha=1 beta=2 %}
  749. ...
  750. {% endwith %}
  751. .. note:: The previous more verbose format is still supported:
  752. ``{% with business.employees.count as total %}``
  753. .. _ref-templates-builtins-filters:
  754. Built-in filter reference
  755. -------------------------
  756. .. templatefilter:: add
  757. add
  758. ~~~
  759. Adds the argument to the value.
  760. For example::
  761. {{ value|add:"2" }}
  762. If ``value`` is ``4``, then the output will be ``6``.
  763. .. versionchanged:: 1.2
  764. The following behavior didn't exist in previous Django versions.
  765. This filter will first try to coerce both values to integers. If this fails,
  766. it'll attempt to add the values together anyway. This will work on some data
  767. types (strings, list, etc.) and fail on others. If it fails, the result will
  768. be an empty string.
  769. For example, if we have::
  770. {{ first|add:second }}
  771. and ``first`` is ``[1, 2, 3]`` and ``second`` is ``[4, 5, 6]``, then the
  772. output will be ``[1, 2, 3, 4, 5, 6]``.
  773. .. warning::
  774. Strings that can be coerced to integers will be **summed**, not
  775. concatenated, as in the first example above.
  776. .. templatefilter:: addslashes
  777. addslashes
  778. ~~~~~~~~~~
  779. Adds slashes before quotes. Useful for escaping strings in CSV, for example.
  780. For example::
  781. {{ value|addslashes }}
  782. If ``value`` is ``"I'm using Django"``, the output will be ``"I\'m using Django"``.
  783. .. templatefilter:: capfirst
  784. capfirst
  785. ~~~~~~~~
  786. Capitalizes the first character of the value.
  787. For example::
  788. {{ value|capfirst }}
  789. If ``value`` is ``"django"``, the output will be ``"Django"``.
  790. .. templatefilter:: center
  791. center
  792. ~~~~~~
  793. Centers the value in a field of a given width.
  794. For example::
  795. "{{ value|center:"15" }}"
  796. If ``value`` is ``"Django"``, the output will be ``" Django "``.
  797. .. templatefilter:: cut
  798. cut
  799. ~~~
  800. Removes all values of arg from the given string.
  801. For example::
  802. {{ value|cut:" "}}
  803. If ``value`` is ``"String with spaces"``, the output will be ``"Stringwithspaces"``.
  804. .. templatefilter:: date
  805. date
  806. ~~~~
  807. Formats a date according to the given format.
  808. Uses the same format as PHP's ``date()`` function (http://php.net/date)
  809. with some custom extensions.
  810. Available format strings:
  811. ================ ======================================== =====================
  812. Format character Description Example output
  813. ================ ======================================== =====================
  814. a ``'a.m.'`` or ``'p.m.'`` (Note that ``'a.m.'``
  815. this is slightly different than PHP's
  816. output, because this includes periods
  817. to match Associated Press style.)
  818. A ``'AM'`` or ``'PM'``. ``'AM'``
  819. b Month, textual, 3 letters, lowercase. ``'jan'``
  820. B Not implemented.
  821. c ISO 8601 Format. ``2008-01-02T10:30:00.000123``
  822. d Day of the month, 2 digits with ``'01'`` to ``'31'``
  823. leading zeros.
  824. D Day of the week, textual, 3 letters. ``'Fri'``
  825. E Month, locale specific alternative
  826. representation usually used for long
  827. date representation. ``'listopada'`` (for Polish locale, as opposed to ``'Listopad'``)
  828. f Time, in 12-hour hours and minutes, ``'1'``, ``'1:30'``
  829. with minutes left off if they're zero.
  830. Proprietary extension.
  831. F Month, textual, long. ``'January'``
  832. g Hour, 12-hour format without leading ``'1'`` to ``'12'``
  833. zeros.
  834. G Hour, 24-hour format without leading ``'0'`` to ``'23'``
  835. zeros.
  836. h Hour, 12-hour format. ``'01'`` to ``'12'``
  837. H Hour, 24-hour format. ``'00'`` to ``'23'``
  838. i Minutes. ``'00'`` to ``'59'``
  839. I Not implemented.
  840. j Day of the month without leading ``'1'`` to ``'31'``
  841. zeros.
  842. l Day of the week, textual, long. ``'Friday'``
  843. L Boolean for whether it's a leap year. ``True`` or ``False``
  844. m Month, 2 digits with leading zeros. ``'01'`` to ``'12'``
  845. M Month, textual, 3 letters. ``'Jan'``
  846. n Month without leading zeros. ``'1'`` to ``'12'``
  847. N Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'``
  848. style. Proprietary extension.
  849. O Difference to Greenwich time in hours. ``'+0200'``
  850. P Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'``
  851. 'a.m.'/'p.m.', with minutes left off
  852. if they're zero and the special-case
  853. strings 'midnight' and 'noon' if
  854. appropriate. Proprietary extension.
  855. r RFC 2822 formatted date. ``'Thu, 21 Dec 2000 16:01:07 +0200'``
  856. s Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'``
  857. S English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'``
  858. month, 2 characters.
  859. t Number of days in the given month. ``28`` to ``31``
  860. T Time zone of this machine. ``'EST'``, ``'MDT'``
  861. u Microseconds. ``0`` to ``999999``
  862. U Seconds since the Unix Epoch
  863. (January 1 1970 00:00:00 UTC).
  864. w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday)
  865. leading zeros.
  866. W ISO-8601 week number of year, with ``1``, ``53``
  867. weeks starting on Monday.
  868. y Year, 2 digits. ``'99'``
  869. Y Year, 4 digits. ``'1999'``
  870. z Day of the year. ``0`` to ``365``
  871. Z Time zone offset in seconds. The ``-43200`` to ``43200``
  872. offset for timezones west of UTC is
  873. always negative, and for those east of
  874. UTC is always positive.
  875. ================ ======================================== =====================
  876. .. versionadded:: 1.2
  877. The ``c`` and ``u`` format specification characters were added in Django 1.2.
  878. For example::
  879. {{ value|date:"D d M Y" }}
  880. If ``value`` is a ``datetime`` object (e.g., the result of
  881. ``datetime.datetime.now()``), the output will be the string
  882. ``'Wed 09 Jan 2008'``.
  883. The format passed can be one of the predefined ones ``DATE_FORMAT``,
  884. ``DATETIME_FORMAT``, ``SHORT_DATE_FORMAT`` or ``SHORT_DATETIME_FORMAT``, or a
  885. custom format that uses the format specifiers shown in the table above. Note
  886. that predefined formats may vary depending on the current locale.
  887. Assuming that :setting:`USE_L10N` is ``True`` and :setting:`LANGUAGE_CODE` is,
  888. for example, ``"es"``, then for::
  889. {{ value|date:"SHORT_DATE_FORMAT" }}
  890. the output would be the string ``"09/01/2008"`` (the ``"SHORT_DATE_FORMAT"``
  891. format specifier for the ``es`` locale as shipped with Django is ``"d/m/Y"``).
  892. When used without a format string::
  893. {{ value|date }}
  894. ...the formatting string defined in the :setting:`DATE_FORMAT` setting will be
  895. used, without applying any localization.
  896. .. versionchanged:: 1.2
  897. Predefined formats can now be influenced by the current locale.
  898. .. templatefilter:: default
  899. default
  900. ~~~~~~~
  901. If value evaluates to ``False``, use given default. Otherwise, use the value.
  902. For example::
  903. {{ value|default:"nothing" }}
  904. If ``value`` is ``""`` (the empty string), the output will be ``nothing``.
  905. .. templatefilter:: default_if_none
  906. default_if_none
  907. ~~~~~~~~~~~~~~~
  908. If (and only if) value is ``None``, use given default. Otherwise, use the
  909. value.
  910. Note that if an empty string is given, the default value will *not* be used.
  911. Use the ``default`` filter if you want to fallback for empty strings.
  912. For example::
  913. {{ value|default_if_none:"nothing" }}
  914. If ``value`` is ``None``, the output will be the string ``"nothing"``.
  915. .. templatefilter:: dictsort
  916. dictsort
  917. ~~~~~~~~
  918. Takes a list of dictionaries and returns that list sorted by the key given in
  919. the argument.
  920. For example::
  921. {{ value|dictsort:"name" }}
  922. If ``value`` is:
  923. .. code-block:: python
  924. [
  925. {'name': 'zed', 'age': 19},
  926. {'name': 'amy', 'age': 22},
  927. {'name': 'joe', 'age': 31},
  928. ]
  929. then the output would be:
  930. .. code-block:: python
  931. [
  932. {'name': 'amy', 'age': 22},
  933. {'name': 'joe', 'age': 31},
  934. {'name': 'zed', 'age': 19},
  935. ]
  936. .. templatefilter:: dictsortreversed
  937. dictsortreversed
  938. ~~~~~~~~~~~~~~~~
  939. Takes a list of dictionaries and returns that list sorted in reverse order by
  940. the key given in the argument. This works exactly the same as the above filter,
  941. but the returned value will be in reverse order.
  942. .. templatefilter:: divisibleby
  943. divisibleby
  944. ~~~~~~~~~~~
  945. Returns ``True`` if the value is divisible by the argument.
  946. For example::
  947. {{ value|divisibleby:"3" }}
  948. If ``value`` is ``21``, the output would be ``True``.
  949. .. templatefilter:: escape
  950. escape
  951. ~~~~~~
  952. Escapes a string's HTML. Specifically, it makes these replacements:
  953. * ``<`` is converted to ``&lt;``
  954. * ``>`` is converted to ``&gt;``
  955. * ``'`` (single quote) is converted to ``&#39;``
  956. * ``"`` (double quote) is converted to ``&quot;``
  957. * ``&`` is converted to ``&amp;``
  958. The escaping is only applied when the string is output, so it does not matter
  959. where in a chained sequence of filters you put ``escape``: it will always be
  960. applied as though it were the last filter. If you want escaping to be applied
  961. immediately, use the ``force_escape`` filter.
  962. Applying ``escape`` to a variable that would normally have auto-escaping
  963. applied to the result will only result in one round of escaping being done. So
  964. it is safe to use this function even in auto-escaping environments. If you want
  965. multiple escaping passes to be applied, use the ``force_escape`` filter.
  966. .. templatefilter:: escapejs
  967. escapejs
  968. ~~~~~~~~
  969. Escapes characters for use in JavaScript strings. This does *not* make the
  970. string safe for use in HTML, but does protect you from syntax errors when using
  971. templates to generate JavaScript/JSON.
  972. For example::
  973. {{ value|escapejs }}
  974. If ``value`` is ``"testing\r\njavascript \'string" <b>escaping</b>"``,
  975. the output will be ``"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E"``.
  976. .. templatefilter:: filesizeformat
  977. filesizeformat
  978. ~~~~~~~~~~~~~~
  979. Format the value like a 'human-readable' file size (i.e. ``'13 KB'``,
  980. ``'4.1 MB'``, ``'102 bytes'``, etc).
  981. For example::
  982. {{ value|filesizeformat }}
  983. If ``value`` is 123456789, the output would be ``117.7 MB``.
  984. .. templatefilter:: first
  985. first
  986. ~~~~~
  987. Returns the first item in a list.
  988. For example::
  989. {{ value|first }}
  990. If ``value`` is the list ``['a', 'b', 'c']``, the output will be ``'a'``.
  991. .. templatefilter:: fix_ampersands
  992. fix_ampersands
  993. ~~~~~~~~~~~~~~
  994. ..note::
  995. This is rarely useful as ampersands are automatically escaped. See escape_ for more information.
  996. Replaces ampersands with ``&amp;`` entities.
  997. For example::
  998. {{ value|fix_ampersands }}
  999. If ``value`` is ``Tom & Jerry``, the output will be ``Tom &amp; Jerry``.
  1000. .. templatefilter:: floatformat
  1001. floatformat
  1002. ~~~~~~~~~~~
  1003. When used without an argument, rounds a floating-point number to one decimal
  1004. place -- but only if there's a decimal part to be displayed. For example:
  1005. ============ =========================== ========
  1006. ``value`` Template Output
  1007. ============ =========================== ========
  1008. ``34.23234`` ``{{ value|floatformat }}`` ``34.2``
  1009. ``34.00000`` ``{{ value|floatformat }}`` ``34``
  1010. ``34.26000`` ``{{ value|floatformat }}`` ``34.3``
  1011. ============ =========================== ========
  1012. If used with a numeric integer argument, ``floatformat`` rounds a number to
  1013. that many decimal places. For example:
  1014. ============ ============================= ==========
  1015. ``value`` Template Output
  1016. ============ ============================= ==========
  1017. ``34.23234`` ``{{ value|floatformat:3 }}`` ``34.232``
  1018. ``34.00000`` ``{{ value|floatformat:3 }}`` ``34.000``
  1019. ``34.26000`` ``{{ value|floatformat:3 }}`` ``34.260``
  1020. ============ ============================= ==========
  1021. If the argument passed to ``floatformat`` is negative, it will round a number
  1022. to that many decimal places -- but only if there's a decimal part to be
  1023. displayed. For example:
  1024. ============ ================================ ==========
  1025. ``value`` Template Output
  1026. ============ ================================ ==========
  1027. ``34.23234`` ``{{ value|floatformat:"-3" }}`` ``34.232``
  1028. ``34.00000`` ``{{ value|floatformat:"-3" }}`` ``34``
  1029. ``34.26000`` ``{{ value|floatformat:"-3" }}`` ``34.260``
  1030. ============ ================================ ==========
  1031. Using ``floatformat`` with no argument is equivalent to using ``floatformat``
  1032. with an argument of ``-1``.
  1033. .. templatefilter:: force_escape
  1034. force_escape
  1035. ~~~~~~~~~~~~
  1036. Applies HTML escaping to a string (see the ``escape`` filter for details).
  1037. This filter is applied *immediately* and returns a new, escaped string. This
  1038. is useful in the rare cases where you need multiple escaping or want to apply
  1039. other filters to the escaped results. Normally, you want to use the ``escape``
  1040. filter.
  1041. .. templatefilter:: get_digit
  1042. get_digit
  1043. ~~~~~~~~~
  1044. Given a whole number, returns the requested digit, where 1 is the right-most
  1045. digit, 2 is the second-right-most digit, etc. Returns the original value for
  1046. invalid input (if input or argument is not an integer, or if argument is less
  1047. than 1). Otherwise, output is always an integer.
  1048. For example::
  1049. {{ value|get_digit:"2" }}
  1050. If ``value`` is ``123456789``, the output will be ``8``.
  1051. .. templatefilter:: iriencode
  1052. iriencode
  1053. ~~~~~~~~~
  1054. Converts an IRI (Internationalized Resource Identifier) to a string that is
  1055. suitable for including in a URL. This is necessary if you're trying to use
  1056. strings containing non-ASCII characters in a URL.
  1057. It's safe to use this filter on a string that has already gone through the
  1058. ``urlencode`` filter.
  1059. For example::
  1060. {{ value|iriencode }}
  1061. If ``value`` is ``"?test=1&me=2"``, the output will be ``"?test=1&amp;me=2"``.
  1062. .. templatefilter:: join
  1063. join
  1064. ~~~~
  1065. Joins a list with a string, like Python's ``str.join(list)``
  1066. For example::
  1067. {{ value|join:" // " }}
  1068. If ``value`` is the list ``['a', 'b', 'c']``, the output will be the string
  1069. ``"a // b // c"``.
  1070. .. templatefilter:: last
  1071. last
  1072. ~~~~
  1073. Returns the last item in a list.
  1074. For example::
  1075. {{ value|last }}
  1076. If ``value`` is the list ``['a', 'b', 'c', 'd']``, the output will be the string
  1077. ``"d"``.
  1078. .. templatefilter:: length
  1079. length
  1080. ~~~~~~
  1081. Returns the length of the value. This works for both strings and lists.
  1082. For example::
  1083. {{ value|length }}
  1084. If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``4``.
  1085. .. templatefilter:: length_is
  1086. length_is
  1087. ~~~~~~~~~
  1088. Returns ``True`` if the value's length is the argument, or ``False`` otherwise.
  1089. For example::
  1090. {{ value|length_is:"4" }}
  1091. If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``True``.
  1092. .. templatefilter:: linebreaks
  1093. linebreaks
  1094. ~~~~~~~~~~
  1095. Replaces line breaks in plain text with appropriate HTML; a single
  1096. newline becomes an HTML line break (``<br />``) and a new line
  1097. followed by a blank line becomes a paragraph break (``</p>``).
  1098. For example::
  1099. {{ value|linebreaks }}
  1100. If ``value`` is ``Joel\nis a slug``, the output will be ``<p>Joel<br />is a
  1101. slug</p>``.
  1102. .. templatefilter:: linebreaksbr
  1103. linebreaksbr
  1104. ~~~~~~~~~~~~
  1105. Converts all newlines in a piece of plain text to HTML line breaks
  1106. (``<br />``).
  1107. For example::
  1108. {{ value|linebreaksbr }}
  1109. If ``value`` is ``Joel\nis a slug``, the output will be ``Joel<br />is a
  1110. slug``.
  1111. .. templatefilter:: linenumbers
  1112. linenumbers
  1113. ~~~~~~~~~~~
  1114. Displays text with line numbers.
  1115. For example::
  1116. {{ value|linenumbers }}
  1117. If ``value`` is::
  1118. one
  1119. two
  1120. three
  1121. the output will be::
  1122. 1. one
  1123. 2. two
  1124. 3. three
  1125. .. templatefilter:: ljust
  1126. ljust
  1127. ~~~~~
  1128. Left-aligns the value in a field of a given width.
  1129. **Argument:** field size
  1130. For example::
  1131. "{{ value|ljust:"10" }}"
  1132. If ``value`` is ``Django``, the output will be ``"Django "``.
  1133. .. templatefilter:: lower
  1134. lower
  1135. ~~~~~
  1136. Converts a string into all lowercase.
  1137. For example::
  1138. {{ value|lower }}
  1139. If ``value`` is ``Still MAD At Yoko``, the output will be ``still mad at yoko``.
  1140. .. templatefilter:: make_list
  1141. make_list
  1142. ~~~~~~~~~
  1143. Returns the value turned into a list. For an integer, it's a list of
  1144. digits. For a string, it's a list of characters.
  1145. For example::
  1146. {{ value|make_list }}
  1147. If ``value`` is the string ``"Joel"``, the output would be the list
  1148. ``[u'J', u'o', u'e', u'l']``. If ``value`` is ``123``, the output will be the
  1149. list ``[1, 2, 3]``.
  1150. .. templatefilter:: phone2numeric
  1151. phone2numeric
  1152. ~~~~~~~~~~~~~
  1153. Converts a phone number (possibly containing letters) to its numerical
  1154. equivalent.
  1155. The input doesn't have to be a valid phone number. This will happily convert
  1156. any string.
  1157. For example::
  1158. {{ value|phone2numeric }}
  1159. If ``value`` is ``800-COLLECT``, the output will be ``800-2655328``.
  1160. .. templatefilter:: pluralize
  1161. pluralize
  1162. ~~~~~~~~~
  1163. Returns a plural suffix if the value is not 1. By default, this suffix is ``'s'``.
  1164. Example::
  1165. You have {{ num_messages }} message{{ num_messages|pluralize }}.
  1166. If ``num_messages`` is ``1``, the output will be ``You have 1 message.``
  1167. If ``num_messages`` is ``2`` the output will be ``You have 2 messages.``
  1168. For words that require a suffix other than ``'s'``, you can provide an alternate
  1169. suffix as a parameter to the filter.
  1170. Example::
  1171. You have {{ num_walruses }} walrus{{ num_walruses|pluralize:"es" }}.
  1172. For words that don't pluralize by simple suffix, you can specify both a
  1173. singular and plural suffix, separated by a comma.
  1174. Example::
  1175. You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}.
  1176. .. templatefilter:: pprint
  1177. pprint
  1178. ~~~~~~
  1179. A wrapper around `pprint.pprint`__ -- for debugging, really.
  1180. __ http://docs.python.org/library/pprint.html
  1181. .. templatefilter:: random
  1182. random
  1183. ~~~~~~
  1184. Returns a random item from the given list.
  1185. For example::
  1186. {{ value|random }}
  1187. If ``value`` is the list ``['a', 'b', 'c', 'd']``, the output could be ``"b"``.
  1188. .. templatefilter:: removetags
  1189. removetags
  1190. ~~~~~~~~~~
  1191. Removes a space-separated list of [X]HTML tags from the output.
  1192. For example::
  1193. {{ value|removetags:"b span"|safe }}
  1194. If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"`` the
  1195. output will be ``"Joel <button>is</button> a slug"``.
  1196. Note that this filter is case-sensitive.
  1197. If ``value`` is ``"<B>Joel</B> <button>is</button> a <span>slug</span>"`` the
  1198. output will be ``"<B>Joel</B> <button>is</button> a slug"``.
  1199. .. templatefilter:: rjust
  1200. rjust
  1201. ~~~~~
  1202. Right-aligns the value in a field of a given width.
  1203. **Argument:** field size
  1204. For example::
  1205. "{{ value|rjust:"10" }}"
  1206. If ``value`` is ``Django``, the output will be ``" Django"``.
  1207. .. templatefilter:: safe
  1208. safe
  1209. ~~~~
  1210. Marks a string as not requiring further HTML escaping prior to output. When
  1211. autoescaping is off, this filter has no effect.
  1212. .. note::
  1213. If you are chaining filters, a filter applied after ``safe`` can
  1214. make the contents unsafe again. For example, the following code
  1215. prints the variable as is, unescaped:
  1216. .. code-block:: html+django
  1217. {{ var|safe|escape }}
  1218. .. templatefilter:: safeseq
  1219. safeseq
  1220. ~~~~~~~
  1221. Applies the :tfilter:`safe` filter to each element of a sequence. Useful in
  1222. conjunction with other filters that operate on sequences, such as
  1223. :tfilter:`join`. For example::
  1224. {{ some_list|safeseq|join:", " }}
  1225. You couldn't use the :tfilter:`safe` filter directly in this case, as it would
  1226. first convert the variable into a string, rather than working with the
  1227. individual elements of the sequence.
  1228. .. templatefilter:: slice
  1229. slice
  1230. ~~~~~
  1231. Returns a slice of the list.
  1232. Uses the same syntax as Python's list slicing. See
  1233. http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
  1234. for an introduction.
  1235. Example::
  1236. {{ some_list|slice:":2" }}
  1237. If ``some_list`` is ``['a', 'b', 'c']``, the output will be ``['a', 'b']``.
  1238. .. templatefilter:: slugify
  1239. slugify
  1240. ~~~~~~~
  1241. Converts to lowercase, removes non-word characters (alphanumerics and
  1242. underscores) and converts spaces to hyphens. Also strips leading and trailing
  1243. whitespace.
  1244. For example::
  1245. {{ value|slugify }}
  1246. If ``value`` is ``"Joel is a slug"``, the output will be ``"joel-is-a-slug"``.
  1247. .. templatefilter:: stringformat
  1248. stringformat
  1249. ~~~~~~~~~~~~
  1250. Formats the variable according to the argument, a string formatting specifier.
  1251. This specifier uses Python string formatting syntax, with the exception that
  1252. the leading "%" is dropped.
  1253. See http://docs.python.org/library/stdtypes.html#string-formatting-operations
  1254. for documentation of Python string formatting
  1255. For example::
  1256. {{ value|stringformat:"s" }}
  1257. If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel is a slug"``.
  1258. .. templatefilter:: striptags
  1259. striptags
  1260. ~~~~~~~~~
  1261. Strips all [X]HTML tags.
  1262. For example::
  1263. {{ value|striptags }}
  1264. If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"``, the
  1265. output will be ``"Joel is a slug"``.
  1266. .. templatefilter:: time
  1267. time
  1268. ~~~~
  1269. Formats a time according to the given format.
  1270. Given format can be the predefined one ``TIME_FORMAT``, or a custom format,
  1271. same as the :tfilter:`date` filter. Note that the predefined format is locale-
  1272. dependant.
  1273. The time filter will only accept parameters in the format string that relate
  1274. to the time of day, not the date (for obvious reasons). If you need to
  1275. format a date, use the :tfilter:`date` filter.
  1276. For example::
  1277. {{ value|time:"H:i" }}
  1278. If ``value`` is equivalent to ``datetime.datetime.now()``, the output will be
  1279. the string ``"01:23"``.
  1280. Another example:
  1281. Assuming that :setting:`USE_L10N` is ``True`` and :setting:`LANGUAGE_CODE` is,
  1282. for example, ``"de"``, then for::
  1283. {{ value|time:"TIME_FORMAT" }}
  1284. the output will be the string ``"01:23:00"`` (The ``"TIME_FORMAT"`` format
  1285. specifier for the ``de`` locale as shipped with Django is ``"H:i:s"``).
  1286. When used without a format string::
  1287. {{ value|time }}
  1288. ...the formatting string defined in the :setting:`TIME_FORMAT` setting will be
  1289. used, without applying any localization.
  1290. .. versionchanged:: 1.2
  1291. Predefined formats can now be influenced by the current locale.
  1292. .. templatefilter:: timesince
  1293. timesince
  1294. ~~~~~~~~~
  1295. Formats a date as the time since that date (e.g., "4 days, 6 hours").
  1296. Takes an optional argument that is a variable containing the date to use as
  1297. the comparison point (without the argument, the comparison point is *now*).
  1298. For example, if ``blog_date`` is a date instance representing midnight on 1
  1299. June 2006, and ``comment_date`` is a date instance for 08:00 on 1 June 2006,
  1300. then ``{{ blog_date|timesince:comment_date }}`` would return "8 hours".
  1301. Comparing offset-naive and offset-aware datetimes will return an empty string.
  1302. Minutes is the smallest unit used, and "0 minutes" will be returned for any
  1303. date that is in the future relative to the comparison point.
  1304. .. templatefilter:: timeuntil
  1305. timeuntil
  1306. ~~~~~~~~~
  1307. Similar to ``timesince``, except that it measures the time from now until the
  1308. given date or datetime. For example, if today is 1 June 2006 and
  1309. ``conference_date`` is a date instance holding 29 June 2006, then
  1310. ``{{ conference_date|timeuntil }}`` will return "4 weeks".
  1311. Takes an optional argument that is a variable containing the date to use as
  1312. the comparison point (instead of *now*). If ``from_date`` contains 22 June
  1313. 2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "1 week".
  1314. Comparing offset-naive and offset-aware datetimes will return an empty string.
  1315. Minutes is the smallest unit used, and "0 minutes" will be returned for any
  1316. date that is in the past relative to the comparison point.
  1317. .. templatefilter:: title
  1318. title
  1319. ~~~~~
  1320. Converts a string into titlecase.
  1321. For example::
  1322. {{ value|title }}
  1323. If ``value`` is ``"my first post"``, the output will be ``"My First Post"``.
  1324. .. templatefilter:: truncatewords
  1325. truncatewords
  1326. ~~~~~~~~~~~~~
  1327. Truncates a string after a certain number of words.
  1328. **Argument:** Number of words to truncate after
  1329. For example::
  1330. {{ value|truncatewords:2 }}
  1331. If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel is ..."``.
  1332. Newlines within the string will be removed.
  1333. .. templatefilter:: truncatewords_html
  1334. truncatewords_html
  1335. ~~~~~~~~~~~~~~~~~~
  1336. Similar to ``truncatewords``, except that it is aware of HTML tags. Any tags
  1337. that are opened in the string and not closed before the truncation point, are
  1338. closed immediately after the truncation.
  1339. This is less efficient than ``truncatewords``, so should only be used when it
  1340. is being passed HTML text.
  1341. For example::
  1342. {{ value|truncatewords_html:2 }}
  1343. If ``value`` is ``"<p>Joel is a slug</p>"``, the output will be
  1344. ``"<p>Joel is ...</p>"``.
  1345. Newlines in the HTML content will be preserved.
  1346. .. templatefilter:: unordered_list
  1347. unordered_list
  1348. ~~~~~~~~~~~~~~
  1349. Recursively takes a self-nested list and returns an HTML unordered list --
  1350. WITHOUT opening and closing <ul> tags.
  1351. The list is assumed to be in the proper format. For example, if ``var`` contains
  1352. ``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``, then
  1353. ``{{ var|unordered_list }}`` would return::
  1354. <li>States
  1355. <ul>
  1356. <li>Kansas
  1357. <ul>
  1358. <li>Lawrence</li>
  1359. <li>Topeka</li>
  1360. </ul>
  1361. </li>
  1362. <li>Illinois</li>
  1363. </ul>
  1364. </li>
  1365. Note: An older, more restrictive and verbose input format is also supported:
  1366. ``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``,
  1367. .. templatefilter:: upper
  1368. upper
  1369. ~~~~~
  1370. Converts a string into all uppercase.
  1371. For example::
  1372. {{ value|upper }}
  1373. If ``value`` is ``"Joel is a slug"``, the output will be ``"JOEL IS A SLUG"``.
  1374. .. templatefilter:: urlencode
  1375. urlencode
  1376. ~~~~~~~~~
  1377. Escapes a value for use in a URL.
  1378. For example::
  1379. {{ value|urlencode }}
  1380. If ``value`` is ``"http://www.example.org/foo?a=b&c=d"``, the output will be
  1381. ``"http%3A//www.example.org/foo%3Fa%3Db%26c%3Dd"``.
  1382. .. versionadded:: 1.3
  1383. An optional argument containing the characters which should not be escaped can
  1384. be provided.
  1385. If not provided, the '/' character is assumed safe. An empty string can be
  1386. provided when *all* characters should be escaped. For example::
  1387. {{ value|urlencode:"" }}
  1388. If ``value`` is ``"http://www.example.org/"``, the output will be
  1389. ``"http%3A%2F%2Fwww.example.org%2F"``.
  1390. .. templatefilter:: urlize
  1391. urlize
  1392. ~~~~~~
  1393. Converts URLs in text into clickable links.
  1394. Works on links beginning with ``http://``, ``https://``, or ``www.`` and
  1395. ending with ``.org``, ``.net`` or ``.com``. Links can have trailing punctuation
  1396. (periods, commas, close-parens) and leading punctuation (opening parens) and
  1397. ``urlize`` will still do the right thing.
  1398. Links generated by ``urlize`` have a ``rel="nofollow"`` attribute added
  1399. to them.
  1400. For example::
  1401. {{ value|urlize }}
  1402. If ``value`` is ``"Check out www.djangoproject.com"``, the output will be
  1403. ``"Check out <a href="http://www.djangoproject.com"
  1404. rel="nofollow">www.djangoproject.com</a>"``.
  1405. The ``urlize`` filter also takes an optional parameter ``autoescape``. If
  1406. ``autoescape`` is ``True``, the link text and URLs will be escaped using
  1407. Django's built-in :tfilter:`escape` filter. The default value for
  1408. ``autoescape`` is ``True``.
  1409. .. note::
  1410. If ``urlize`` is applied to text that already contains HTML markup,
  1411. things won't work as expected. Apply this filter only to plain text.
  1412. .. templatefilter:: urlizetrunc
  1413. urlizetrunc
  1414. ~~~~~~~~~~~
  1415. Converts URLs into clickable links just like urlize_, but truncates URLs
  1416. longer than the given character limit.
  1417. **Argument:** Number of characters that link text should be truncated to,
  1418. including the ellipsis that's added if truncation is necessary.
  1419. For example::
  1420. {{ value|urlizetrunc:15 }}
  1421. If ``value`` is ``"Check out www.djangoproject.com"``, the output would be
  1422. ``'Check out <a href="http://www.djangoproject.com"
  1423. rel="nofollow">www.djangopr...</a>'``.
  1424. As with urlize_, this filter should only be applied to plain text.
  1425. .. templatefilter:: wordcount
  1426. wordcount
  1427. ~~~~~~~~~
  1428. Returns the number of words.
  1429. For example::
  1430. {{ value|wordcount }}
  1431. If ``value`` is ``"Joel is a slug"``, the output will be ``4``.
  1432. .. templatefilter:: wordwrap
  1433. wordwrap
  1434. ~~~~~~~~
  1435. Wraps words at specified line length.
  1436. **Argument:** number of characters at which to wrap the text
  1437. For example::
  1438. {{ value|wordwrap:5 }}
  1439. If ``value`` is ``Joel is a slug``, the output would be::
  1440. Joel
  1441. is a
  1442. slug
  1443. .. templatefilter:: yesno
  1444. yesno
  1445. ~~~~~
  1446. Given a string mapping values for true, false and (optionally) None,
  1447. returns one of those strings according to the value:
  1448. For example::
  1449. {{ value|yesno:"yeah,no,maybe" }}
  1450. ========== ====================== ==================================
  1451. Value Argument Outputs
  1452. ========== ====================== ==================================
  1453. ``True`` ``"yeah,no,maybe"`` ``yeah``
  1454. ``False`` ``"yeah,no,maybe"`` ``no``
  1455. ``None`` ``"yeah,no,maybe"`` ``maybe``
  1456. ``None`` ``"yeah,no"`` ``"no"`` (converts None to False
  1457. if no mapping for None is given)
  1458. ========== ====================== ==================================
  1459. Other tags and filter libraries
  1460. -------------------------------
  1461. Django comes with a couple of other template-tag libraries that you have to
  1462. enable explicitly in your ``INSTALLED_APPS`` setting and enable in your
  1463. template with the ``{% load %}`` tag.
  1464. django.contrib.humanize
  1465. ~~~~~~~~~~~~~~~~~~~~~~~
  1466. A set of Django template filters useful for adding a "human touch" to data. See
  1467. :doc:`/ref/contrib/humanize`.
  1468. django.contrib.markup
  1469. ~~~~~~~~~~~~~~~~~~~~~
  1470. A collection of template filters that implement these common markup languages:
  1471. * Textile
  1472. * Markdown
  1473. * reST (reStructuredText)
  1474. See the :doc:`markup documentation </ref/contrib/markup>`.
  1475. django.contrib.webdesign
  1476. ~~~~~~~~~~~~~~~~~~~~~~~~
  1477. A collection of template tags that can be useful while designing a Web site,
  1478. such as a generator of Lorem Ipsum text. See :doc:`/ref/contrib/webdesign`.
  1479. i18n
  1480. ~~~~
  1481. Provides a couple of templatetags that allow specifying translatable text in
  1482. Django templates. It is slightly different from the libraries described
  1483. above because you don't need to add any application to the ``INSTALLED_APPS``
  1484. setting but rather set :setting:`USE_I18N` to True, then loading it with
  1485. ``{% load i18n %}``. See :ref:`specifying-translation-strings-in-template-code`.
  1486. l10n
  1487. ~~~~
  1488. Provides a couple of templatetags that allow control over the localization of
  1489. values in Django templates. It is slightly different from the libraries described
  1490. above because you don't need to add any application to the ``INSTALLED_APPS``;
  1491. you only need to load the library using ``{% load l10n %}``. See
  1492. :ref:`topic-l10n-templates`.