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