1204
1267
{{ value|cut:" "}}
1206
If ``value`` is ``"String with spaces"``, the output will be ``"Stringwithspaces"``.
1269
If ``value`` is ``"String with spaces"``, the output will be
1270
``"Stringwithspaces"``.
1208
1272
.. templatefilter:: date
1213
1277
Formats a date according to the given format.
1215
Uses the same format as PHP's ``date()`` function (http://php.net/date)
1216
with some custom extensions.
1279
Uses a similar format as PHP's ``date()`` function (http://php.net/date)
1280
with some differences.
1218
1282
Available format strings:
1220
================ ======================================== =====================
1221
Format character Description Example output
1222
================ ======================================== =====================
1223
a ``'a.m.'`` or ``'p.m.'`` (Note that ``'a.m.'``
1224
this is slightly different than PHP's
1225
output, because this includes periods
1226
to match Associated Press style.)
1227
A ``'AM'`` or ``'PM'``. ``'AM'``
1228
b Month, textual, 3 letters, lowercase. ``'jan'``
1230
c ISO 8601 Format. ``2008-01-02T10:30:00.000123``
1231
d Day of the month, 2 digits with ``'01'`` to ``'31'``
1233
D Day of the week, textual, 3 letters. ``'Fri'``
1234
E Month, locale specific alternative
1235
representation usually used for long
1236
date representation. ``'listopada'`` (for Polish locale, as opposed to ``'Listopad'``)
1237
f Time, in 12-hour hours and minutes, ``'1'``, ``'1:30'``
1238
with minutes left off if they're zero.
1239
Proprietary extension.
1240
F Month, textual, long. ``'January'``
1241
g Hour, 12-hour format without leading ``'1'`` to ``'12'``
1243
G Hour, 24-hour format without leading ``'0'`` to ``'23'``
1245
h Hour, 12-hour format. ``'01'`` to ``'12'``
1246
H Hour, 24-hour format. ``'00'`` to ``'23'``
1247
i Minutes. ``'00'`` to ``'59'``
1249
j Day of the month without leading ``'1'`` to ``'31'``
1251
l Day of the week, textual, long. ``'Friday'``
1252
L Boolean for whether it's a leap year. ``True`` or ``False``
1253
m Month, 2 digits with leading zeros. ``'01'`` to ``'12'``
1254
M Month, textual, 3 letters. ``'Jan'``
1255
n Month without leading zeros. ``'1'`` to ``'12'``
1256
N Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'``
1257
style. Proprietary extension.
1258
O Difference to Greenwich time in hours. ``'+0200'``
1259
P Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'``
1260
'a.m.'/'p.m.', with minutes left off
1261
if they're zero and the special-case
1262
strings 'midnight' and 'noon' if
1263
appropriate. Proprietary extension.
1264
r RFC 2822 formatted date. ``'Thu, 21 Dec 2000 16:01:07 +0200'``
1265
s Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'``
1266
S English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'``
1267
month, 2 characters.
1268
t Number of days in the given month. ``28`` to ``31``
1269
T Time zone of this machine. ``'EST'``, ``'MDT'``
1270
u Microseconds. ``0`` to ``999999``
1271
U Seconds since the Unix Epoch
1272
(January 1 1970 00:00:00 UTC).
1273
w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday)
1275
W ISO-8601 week number of year, with ``1``, ``53``
1276
weeks starting on Monday.
1277
y Year, 2 digits. ``'99'``
1278
Y Year, 4 digits. ``'1999'``
1279
z Day of the year. ``0`` to ``365``
1280
Z Time zone offset in seconds. The ``-43200`` to ``43200``
1281
offset for timezones west of UTC is
1282
always negative, and for those east of
1283
UTC is always positive.
1284
================ ======================================== =====================
1284
================ ======================================== =====================
1285
Format character Description Example output
1286
================ ======================================== =====================
1287
a ``'a.m.'`` or ``'p.m.'`` (Note that ``'a.m.'``
1288
this is slightly different than PHP's
1289
output, because this includes periods
1290
to match Associated Press style.)
1291
A ``'AM'`` or ``'PM'``. ``'AM'``
1292
b Month, textual, 3 letters, lowercase. ``'jan'``
1294
c ISO 8601 format. (Note: unlike others ``2008-01-02T10:30:00.000123+02:00``,
1295
formatters, such as "Z", "O" or "r", or ``2008-01-02T10:30:00.000123`` if the datetime is naive
1296
the "c" formatter will not add timezone
1297
offset if value is a naive datetime
1298
(see :class:`datetime.tzinfo`).
1299
d Day of the month, 2 digits with ``'01'`` to ``'31'``
1301
D Day of the week, textual, 3 letters. ``'Fri'``
1302
e Timezone name. Could be in any format,
1303
or might return an empty string, ``''``, ``'GMT'``, ``'-500'``, ``'US/Eastern'``, etc.
1304
depending on the datetime.
1305
E Month, locale specific alternative
1306
representation usually used for long
1307
date representation. ``'listopada'`` (for Polish locale, as opposed to ``'Listopad'``)
1308
f Time, in 12-hour hours and minutes, ``'1'``, ``'1:30'``
1309
with minutes left off if they're zero.
1310
Proprietary extension.
1311
F Month, textual, long. ``'January'``
1312
g Hour, 12-hour format without leading ``'1'`` to ``'12'``
1314
G Hour, 24-hour format without leading ``'0'`` to ``'23'``
1316
h Hour, 12-hour format. ``'01'`` to ``'12'``
1317
H Hour, 24-hour format. ``'00'`` to ``'23'``
1318
i Minutes. ``'00'`` to ``'59'``
1320
j Day of the month without leading ``'1'`` to ``'31'``
1322
l Day of the week, textual, long. ``'Friday'``
1323
L Boolean for whether it's a leap year. ``True`` or ``False``
1324
m Month, 2 digits with leading zeros. ``'01'`` to ``'12'``
1325
M Month, textual, 3 letters. ``'Jan'``
1326
n Month without leading zeros. ``'1'`` to ``'12'``
1327
N Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'``
1328
style. Proprietary extension.
1329
o ISO-8601 week-numbering year, ``'1999'``
1331
the ISO-8601 week number (W)
1332
O Difference to Greenwich time in hours. ``'+0200'``
1333
P Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'``
1334
'a.m.'/'p.m.', with minutes left off
1335
if they're zero and the special-case
1336
strings 'midnight' and 'noon' if
1337
appropriate. Proprietary extension.
1338
r :rfc:`2822` formatted date. ``'Thu, 21 Dec 2000 16:01:07 +0200'``
1339
s Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'``
1340
S English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'``
1341
month, 2 characters.
1342
t Number of days in the given month. ``28`` to ``31``
1343
T Time zone of this machine. ``'EST'``, ``'MDT'``
1344
u Microseconds. ``0`` to ``999999``
1345
U Seconds since the Unix Epoch
1346
(January 1 1970 00:00:00 UTC).
1347
w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday)
1349
W ISO-8601 week number of year, with ``1``, ``53``
1350
weeks starting on Monday.
1351
y Year, 2 digits. ``'99'``
1352
Y Year, 4 digits. ``'1999'``
1353
z Day of the year. ``0`` to ``365``
1354
Z Time zone offset in seconds. The ``-43200`` to ``43200``
1355
offset for timezones west of UTC is
1356
always negative, and for those east of
1357
UTC is always positive.
1358
================ ======================================== =====================
1286
1360
.. versionadded:: 1.2
1288
1362
The ``c`` and ``u`` format specification characters were added in Django 1.2.
1364
.. versionadded:: 1.4
1366
The ``e`` and ``o`` format specification characters were added in Django 1.4.
1292
1370
{{ value|date:"D d M Y" }}
2229
2350
if no mapping for None is given)
2230
2351
========== ====================== ==================================
2232
Other tags and filter libraries
2233
-------------------------------
2353
Internationalization tags and filters
2354
-------------------------------------
2356
Django provides template tags and filters to control each aspect of
2357
:doc:`internationalization </topics/i18n/index>` in templates. They allow for
2358
granular control of translations, formatting, and time zone conversions.
2363
This library allows specifying translatable text in templates.
2364
To enable it, set :setting:`USE_I18N` to ``True``, then load it with
2365
``{% load i18n %}``.
2367
See :ref:`specifying-translation-strings-in-template-code`.
2372
This library provides control over the localization of values in templates.
2373
You only need to load the library using ``{% load l10n %}``, but you'll often
2374
set :setting:`USE_L10N` to ``True`` so that localization is active by default.
2376
See :ref:`topic-l10n-templates`.
2381
.. versionadded:: 1.4
2383
This library provides control over time zone conversions in templates.
2384
Like ``l10n``, you only need to load the library using ``{% load tz %}``,
2385
but you'll usually also set :setting:`USE_TZ` to ``True`` so that conversion
2386
to local time happens by default.
2388
See :ref:`time-zones-in-templates`.
2390
Other tags and filters libraries
2391
--------------------------------
2235
2393
Django comes with a couple of other template-tag libraries that you have to
2236
2394
enable explicitly in your :setting:`INSTALLED_APPS` setting and enable in your
2237
template with the ``{% load %}`` tag.
2395
template with the :ttag:`{% load %}<load>` tag.
2239
2397
django.contrib.humanize
2240
~~~~~~~~~~~~~~~~~~~~~~~
2398
^^^^^^^^^^^^^^^^^^^^^^^
2242
2400
A set of Django template filters useful for adding a "human touch" to data. See
2243
2401
:doc:`/ref/contrib/humanize`.
2245
2403
django.contrib.markup
2246
~~~~~~~~~~~~~~~~~~~~~
2404
^^^^^^^^^^^^^^^^^^^^^
2248
2406
A collection of template filters that implement these common markup languages:
2252
* reST (reStructuredText)
2410
* reST (reStructuredText)
2254
2412
See the :doc:`markup documentation </ref/contrib/markup>`.
2256
2414
django.contrib.webdesign
2257
~~~~~~~~~~~~~~~~~~~~~~~~
2415
^^^^^^^^^^^^^^^^^^^^^^^^
2259
2417
A collection of template tags that can be useful while designing a Web site,
2260
2418
such as a generator of Lorem Ipsum text. See :doc:`/ref/contrib/webdesign`.
2265
Provides a couple of templatetags that allow specifying translatable text in
2266
Django templates. It is slightly different from the libraries described
2267
above because you don't need to add any application to the
2268
:setting:`INSTALLED_APPS` setting but rather set :setting:`USE_I18N` to True,
2269
then loading it with ``{% load i18n %}``.
2271
See :ref:`specifying-translation-strings-in-template-code`.
2276
Provides a couple of templatetags that allow control over the localization of
2277
values in Django templates. It is slightly different from the libraries described
2278
above because you don't need to add any application to the :setting:`INSTALLED_APPS`;
2279
you only need to load the library using ``{% load l10n %}``.
2281
See :ref:`topic-l10n-templates`.
2423
.. templatetag:: static
2428
.. highlight:: html+django
2430
To link to static files that are saved in :setting:`STATIC_ROOT` Django ships
2431
with a :ttag:`static` template tag. You can use this regardless if you're
2432
using :class:`~django.template.RequestContext` or not.
2434
.. code-block:: html+django
2437
<img src="{% static "images/hi.jpg" %}" />
2439
It is also able to consume standard context variables, e.g. assuming a
2440
``user_stylesheet`` variable is passed to the template:
2442
.. code-block:: html+django
2445
<link rel="stylesheet" href="{% static user_stylesheet %}" type="text/css" media="screen" />
2449
The :mod:`staticfiles<django.contrib.staticfiles>` contrib app also ships
2450
with a :ttag:`static template tag<staticfiles-static>` which uses
2451
``staticfiles'`` :setting:`STATICFILES_STORAGE` to build the URL of the
2452
given path. Use that instead if you have an advanced use case such as
2453
:ref:`using a cloud service to serve static files<staticfiles-from-cdn>`::
2455
{% load static from staticfiles %}
2456
<img src="{% static "images/hi.jpg" %}" />
2458
.. templatetag:: get_static_prefix
2463
.. highlight:: html+django
2465
If you're not using :class:`~django.template.RequestContext`, or if you need
2466
more control over exactly where and how :setting:`STATIC_URL` is injected
2467
into the template, you can use the :ttag:`get_static_prefix` template tag
2471
<img src="{% get_static_prefix %}images/hi.jpg" />
2473
There's also a second form you can use to avoid extra processing if you need
2474
the value multiple times::
2477
{% get_static_prefix as STATIC_PREFIX %}
2479
<img src="{{ STATIC_PREFIX }}images/hi.jpg" />
2480
<img src="{{ STATIC_PREFIX }}images/hi2.jpg" />
2482
.. templatetag:: get_media_prefix
2487
.. highlight:: html+django
2489
Similar to the :ttag:`get_static_prefix`, ``get_media_prefix`` populates a
2490
template variable with the media prefix :setting:`MEDIA_URL`, e.g.::
2492
<script type="text/javascript" charset="utf-8">
2493
var media_path = '{% get_media_prefix %}';