Search code examples
djangohttp-caching

Django prevent caching view if user is logged in


My visitors get a cached version of the page from Varnish. I would like the admin user to see the current version of the page all times. This way, all changes are directly visible.

Does someting like that exist? I'm aware of the @never_cache decorator. I'm looking for something like that, only if the user is not logged in.

Bonus points if it works with Django-CMS!


Solution

  • I found out there is something like:

    CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
    

    That should work for most web sites, if there is no distinction between users and staff members.

    To explicitly send a Cache-Control header for staff members, some middleware can be written (which also varies on Cookie header sadly).