101
113
Values enclosed in single (``'``) or double quotes (``"``) are treated as
102
114
string literals, while values without quotes are treated as template variables.
104
Note that the variables included in the cycle will not be escaped. This is
105
because template tags do not escape their content. If you want to escape the
106
variables in the cycle, you must do so explicitly::
116
Note that the variables included in the cycle will not be escaped.
117
This is because template tags do not escape their content. Any HTML or
118
Javascript code contained in the printed variable will be rendered
119
as-is, which could potentially lead to security issues.
121
If you need to escape the variables in the cycle, you must do so
108
124
{% filter force_escape %}
109
125
{% cycle var1 var2 var3 %}
192
208
{% firstof var1 var2 var3 "fallback value" %}
194
Note that the variables included in the firstof tag will not be escaped. This
195
is because template tags do not escape their content. If you want to escape
196
the variables in the firstof tag, you must do so explicitly::
210
Note that the variables included in the firstof tag will not be
211
escaped. This is because template tags do not escape their content.
212
Any HTML or Javascript code contained in the printed variable will be
213
rendered as-is, which could potentially lead to security issues.
215
If you need to escape the variables in the firstof tag, you must do so
198
218
{% filter force_escape %}
199
219
{% firstof var1 var2 var3 "fallback value" %}
326
349
There are some athletes and absolutely no coaches.
329
``if`` tags don't allow ``and`` and ``or`` clauses within the same tag, because
330
the order of logic would be ambiguous. For example, this is invalid::
352
.. versionchanged:: 1.2
354
Use of both ``and`` and ``or`` clauses within the same tag is allowed, with
355
``and`` having higher precedence than ``or`` e.g.::
332
357
{% if athlete_list and coach_list or cheerleader_list %}
334
If you need to combine ``and`` and ``or`` to do advanced logic, just use nested
335
``if`` tags. For example::
337
{% if athlete_list %}
338
{% if coach_list or cheerleader_list %}
339
We have athletes, and either coaches or cheerleaders!
343
Multiple uses of the same logical operator are fine, as long as you use the
344
same operator. For example, this is valid::
346
{% if athlete_list or coach_list or parent_list or teacher_list %}
359
will be interpreted like:
361
.. code-block:: python
363
if (athlete_list and coach_list) or cheerleader_list
365
Use of actual brackets in the ``if`` tag is invalid syntax. If you need them to
366
indicate precedence, you should use nested ``if`` tags.
368
.. versionadded:: 1.2
371
``if`` tags may also use the operators ``==``, ``!=``, ``<``, ``>``,
372
``<=``, ``>=`` and ``in`` which work as follows:
380
{% if somevar == "x" %}
381
This appears if variable somevar equals the string "x"
387
Inequality. Example::
389
{% if somevar != "x" %}
390
This appears if variable somevar does not equal the string "x",
391
or if somevar is not found in the context
399
{% if somevar < 100 %}
400
This appears if variable somevar is less than 100.
406
Greater than. Example::
409
This appears if variable somevar is greater than 0.
415
Less than or equal to. Example::
417
{% if somevar <= 100 %}
418
This appears if variable somevar is less than 100 or equal to 100.
424
Greater than or equal to. Example::
426
{% if somevar >= 1 %}
427
This appears if variable somevar is greater than 1 or equal to 1.
433
Contained within. This operator is supported by many Python containers to test
434
whether the given value is in the container. The following are some examples of
435
how ``x in y`` will be interpreted::
437
{% if "bc" in "abcdef" %}
438
This appears since "bc" is a substring of "abcdef"
441
{% if "hello" in greetings %}
442
If greetings is a list or set, one element of which is the string
443
"hello", this will appear.
446
{% if user in users %}
447
If users is a QuerySet, this will appear if user is an
448
instance that belongs to the QuerySet.
454
Not contained within. This is the negation of the ``in`` operator.
457
The comparison operators cannot be 'chained' like in Python or in mathematical
458
notation. For example, instead of using::
460
{% if a > b > c %} (WRONG)
464
{% if a > b and b > c %}
470
You can also use filters in the ``if`` expression. For example::
472
{% if messages|length >= 100 %}
473
You have lots of messages today!
479
All of the above can be combined to form complex expressions. For such
480
expressions, it can be important to know how the operators are grouped when the
481
expression is evaluated - that is, the precedence rules. The precedence of the
482
operators, from lowest to highest, is as follows:
488
* ``==``, ``!=``, ``<``, ``>``,``<=``, ``>=``
490
(This follows Python exactly). So, for example, the following complex if tag:
492
{% if a == b or c == d and e %}
494
...will be interpreted as:
496
.. code-block:: python
498
(a == b) or ((c == d) and e)
500
If you need different precedence, you will need to use nested if tags. Sometimes
501
that is better for clarity anyway, for the sake of those who do not know the
348
505
.. templatetag:: ifchanged
739
915
view function and optional parameters. This is a way to output links without
740
916
violating the DRY principle by having to hard-code URLs in your templates::
742
{% url path.to.some_view arg1,arg2,name1=value1 %}
918
{% url path.to.some_view v1 v2 %}
744
920
The first argument is a path to a view function in the format
745
921
``package.package.module.function``. Additional arguments are optional and
746
should be comma-separated values that will be used as positional and keyword
747
arguments in the URL. All arguments required by the URLconf should be present.
922
should be space-separated values that will be used as arguments in the URL.
923
The example above shows passing positional arguments. Alternatively you may
926
{% url path.to.some_view arg1=v1 arg2=v2 %}
928
Do not mix both positional and keyword syntax in a single call. All arguments
929
required by the URLconf should be present.
749
931
For example, suppose you have a view, ``app_views.client``, whose URLconf
750
932
takes a client ID (here, ``client()`` is a method inside the views file
805
987
<topics-http-reversing-url-namespaces>`, including using any hints provided
806
988
by the context as to the current application.
990
.. versionchanged:: 1.2
992
For backwards compatibility, the ``{% url %}`` tag also supports the
993
use of commas to separate arguments. You shouldn't use this in any new
994
projects, but for the sake of the people who are still using it,
995
here's what it looks like::
997
{% url path.to.view arg,arg2 %}
998
{% url path.to.view arg, arg2 %}
1000
This syntax doesn't support the use of literal commas, or or equals
1001
signs. Did we mention you shouldn't use this syntax in any new
808
1004
.. templatetag:: widthratio
859
1055
If ``value`` is ``4``, then the output will be ``6``.
1057
.. versionchanged:: 1.2
1058
The following behavior didn't exist in previous Django versions.
1060
This filter will first try to coerce both values to integers. If this fails,
1061
it'll attempt to add the values together anyway. This will work on some data
1062
types (strings, list, etc.) and fail on others. If it fails, the result will
1065
For example, if we have::
1067
{{ first|add:second }}
1069
and ``first`` is ``[1, 2, 3]`` and ``second`` is ``[4, 5, 6]``, then the
1070
output will be ``[1, 2, 3, 4, 5, 6]``.
1074
Keep in mind that strings that can both be coerced to integers will be,
1075
and thus will be will be *summed*, not concatenated, as in the first
861
1078
.. templatefilter:: addslashes
907
1147
``datetime.datetime.now()``), the output will be the string
908
1148
``'Wed 09 Jan 2008'``.
1152
Assuming that :setting:`USE_L10N` is ``True`` and :setting:`LANGUAGE_CODE` is,
1153
for example, ``"es"``, then for::
1155
{{ value|date:"SHORT_DATE_FORMAT" }}
1157
the output will be the string ``"09/01/2008"`` (The ``"SHORT_DATE_FORMAT"``
1158
format specifier for the ``es`` locale as shipped with Django is ``"d/m/Y"``).
910
1160
When used without a format string::
912
1162
{{ value|date }}
914
1164
...the formatting string defined in the :setting:`DATE_FORMAT` setting will be
1165
used, without applying any localization.
1167
.. versionchanged:: 1.2
1168
Predefined formats can now be influenced by the current locale.
917
1170
.. templatefilter:: default
1472
1798
If ``value`` is equivalent to ``datetime.datetime.now()``, the output will be
1473
1799
the string ``"01:23"``.
1803
Assuming that :setting:`USE_L10N` is ``True`` and :setting:`LANGUAGE_CODE` is,
1804
for example, ``"de"``, then for::
1806
{{ value|time:"TIME_FORMAT" }}
1808
the output will be the string ``"01:23:00"`` (The ``"TIME_FORMAT"`` format
1809
specifier for the ``de`` locale as shipped with Django is ``"H:i:s"``).
1475
1811
When used without a format string::
1477
1813
{{ value|time }}
1479
1815
...the formatting string defined in the :setting:`TIME_FORMAT` setting will be
1816
used, without applying any localization.
1818
.. versionchanged:: 1.2
1819
Predefined formats can now be influenced by the current locale.
1482
1821
.. templatefilter:: timesince