在django中为匿名用户进行缓存 [英] Caching for anonymous users in django
问题描述
我如何去匿名用户的缓存页面,但是为Django 1.6中的授权用户呈现?以前是一个听起来很完美的CACHE_MIDDLEWARE_ANONYMOUS_ONLY标志,但已经被删除了。
How would I go about caching pages for anonymous users but rendering them for authorized users in Django 1.6? There used to be a CACHE_MIDDLEWARE_ANONYMOUS_ONLY flag that sounded perfect, but that has gotten removed.
我问,因为每个页面都有一个菜单栏,显示登录的用户名并链接到他/她的个人资料。
I'm asking because every page has a menu bar that displays the logged in user's name and a link to his/her profile.
正确的方式是什么?必须是一个常见的问题,但是我没有找到正确的方法来查看Django文档。
What's the correct way of doing this? Must be a common problem, but I haven't found the right way from looking through the Django documentation.
推荐答案
需要视图中的任何代码:
this does not require any code in a view:
{% with cache_timeout=user.is_staff|yesno:"0,300" %}
{% cache cache_timeout cacheidentifier user.is_staff %}
your content here
{% endcache %}
{% endwith %}
这篇关于在django中为匿名用户进行缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!