djangocachingdjango-templatesdjango-cacheanonymous-users

Caching for anonymous users in django


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.

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.


Solution

  • 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 %}