Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 584
Nixarn Avatar asked Jan 19 '14 00:01

Nixarn


1 Answers

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 %}
like image 153
panosmm Avatar answered Sep 20 '22 18:09

panosmm