你同样可以使用cache标签来缓存模板片段。 在模板的顶端附近加入{% load cache %}以通知模板存取缓存标签。
模板标签{% cache %}在给定的时间内缓存了块的内容。 它至少需要两个参数: 缓存超时时间(以秒计)和指定缓存片段的名称。 示例:
{% load cache %}
{% cache 500 sidebar %}
.. sidebar ..
{% endcache %}
Copier après la connexion
有时你可能想缓存基于片段的动态内容的多份拷贝。 比如,你想为上一个例子的每个用户分别缓存侧边栏。 这样只需要给{% cache %}传递额外的参数以标识缓存片段。
{% load cache %}
{% cache 500 sidebar request.user.username %}
.. sidebar for logged in user ..
{% endcache %}
Copier après la connexion
传递不止一个参数也是可行的。 简单地把参数传给{% cache %}。
缓存超时时间可以作为模板变量,只要它可以解析为整数值。 例如,如果模板变量my_timeout值为600,那么以下两个例子是等价的。
{% cache 600 sidebar %} ... {% endcache %}
{% cache my_timeout sidebar %} ... {% endcache %}
Copier après la connexion
这个特性在避免模板重复方面非常有用。 可以把超时时间保存在变量里,然后在别的地方复用。
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
-
2025-02-26 03:58:14
-
2025-02-26 03:38:10
-
2025-02-26 03:17:10
-
2025-02-26 02:49:09
-
2025-02-26 01:08:13
-
2025-02-26 00:46:10
-
2025-02-25 23:42:08
-
2025-02-25 22:50:13
-
2025-02-25 21:54:11
-
2025-02-25 20:45:11
Derniers numéros
-
2025-03-26 16:39:41
-
2025-03-26 16:38:35
-
2025-03-26 16:37:42
-
2025-03-26 16:36:43
-
2025-03-26 16:35:41