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.
I'm not sure if this is the 'correct' way of achieving this but I am using the {% cache %} template tag to get around this problem. The dynamic username bit of the template is in my base template and I cache the rest as below:
By specifying a 'key-name' you can then use the below in a view to clear out the cache if you need to refresh manually:
You can use the following approach by creating a decorator:
then in your urls:
source: http://codeinpython.blogspot.com/2017/01/caching-for-anonymous-non-authenticated.html
context = {"cache_timeout": 300 if request.user.is_anonymous() else 0}
{% load cache %}
{% cache cache_timeout "my-cache-fragment" %}
I have to write this only once
{% endcache %}
this does not require any code in a view: