@@ -320,6 +320,8 @@ time, rather than ``CACHE_MIDDLEWARE_SECONDS``. Using the decorators in
320
320
the ``never_cache`` decorator). See the `using other headers`__ section for
321
321
more on these decorators.
322
322
323
+ .. _i18n-cache-key:
324
+
323
325
.. versionadded:: 1.2
324
326
325
327
If :setting:`USE_I18N` is set to ``True`` then the generated cache key will
@@ -413,7 +415,9 @@ the ``cache`` template tag. To give your template access to this tag, put
413
415
414
416
The ``{% cache %}`` template tag caches the contents of the block for a given
415
417
amount of time. It takes at least two arguments: the cache timeout , in seconds ,
416
- and the name to give the cache fragment. For example::
418
+ and the name to give the cache fragment. For example:
419
+
420
+ .. code-block:: html+django
417
421
418
422
{% load cache %}
419
423
{% cache 500 sidebar %}
@@ -424,7 +428,9 @@ Sometimes you might want to cache multiple copies of a fragment depending on
424
428
some dynamic data that appears inside the fragment. For example , you might want a
425
429
separate cached copy of the sidebar used in the previous example for every user
426
430
of your site. Do this by passing additional arguments to the ``{% cache %}``
427
- template tag to uniquely identify the cache fragment::
431
+ template tag to uniquely identify the cache fragment:
432
+
433
+ .. code-block:: html+django
428
434
429
435
{% load cache %}
430
436
{% cache 500 sidebar request.user.username %}
@@ -434,10 +440,29 @@ template tag to uniquely identify the cache fragment::
434
440
It's perfectly fine to specify more than one argument to identify the fragment.
435
441
Simply pass as many arguments to ``{% cache %}`` as you need.
436
442
443
+ If :setting:`USE_I18N` is set to ``True`` the per-site middleware cache will
444
+ :ref:`respect the active language<i18n-cache-key>`. For the ``cache`` template
445
+ tag you could use one of the
446
+ :ref:`translation-specific variables<template-translation-vars>` available in
447
+ templates to archieve the same result:
448
+
449
+ .. code-block:: html+django
450
+
451
+ {% load i18n %}
452
+ {% load cache %}
453
+
454
+ {% get_current_language as LANGUAGE_CODE %}
455
+
456
+ {% cache 600 welcome LANGUAGE_CODE %}
457
+ {% trans "Welcome to example.com" %}
458
+ {% endcache %}
459
+
437
460
The cache timeout can be a template variable , as long as the template variable
438
461
resolves to an integer value. For example , if the template variable
439
462
``my_timeout`` is set to the value ``600`` , then the following two examples are
440
- equivalent::
463
+ equivalent:
464
+
465
+ .. code-block:: html+django
441
466
442
467
{% cache 600 sidebar %} ... {% endcache %}
443
468
{% cache my_timeout sidebar %} ... {% endcache %}
@@ -448,6 +473,8 @@ timeout in a variable, in one place, and just reuse that value.
448
473
The low-level cache API
449
474
=======================
450
475
476
+ .. highlight:: python
477
+
451
478
Sometimes , caching an entire rendered page doesn't gain you very much and is ,
452
479
in fact , inconvenient overkill.
453
480
0 commit comments