我想为匿名用户使用 django 片段缓存,但为经过身份验证的用户提供新鲜数据。这似乎工作正常:
{% if user.is_anonymous %}
{% load cache %}
{% cache 300 "my-cache-fragment" %}
<b>I have to write this out twice</b>
{% endcache %}
{% else %}
<b>I have to write this out twice</b>
{% endif %}
唯一的问题是我必须重复要缓存的 html。除了把它放在一个包含中之外,还有什么聪明的方法可以解决这个问题吗?谢谢。