A django Middleware that will help to set cache-control header on the views.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
smart_cache_control fix small thinko Jun 24, 2015
.gitignore Initial commit Feb 1, 2015
LICENSE Initial commit Feb 1, 2015
MANIFEST Added distills file. Feb 1, 2015
README.md make VARY_HEADERS a list, with sane defaults Jun 24, 2015
setup.py New version Feb 11, 2015



Set some cache-related headers automatically, defining not-authenticated requests as public and authenticated requests as private. You can also customize these values for specific URLs. This middleware class will also setup these HTTP headers:

  • Vary
  • Cache-Control
  • Expires

This middleware works and is designed to integrate Django with CloudFlare Cache (or similar services). Used in production on MinbilDinbil.dk servers.


Add django-smartcc on your requirements.txt file or just launch:

pip install -U django-smartcc

Then add django-smartcc in the installed apps and in the middleware, so add these line in your settings.py file.

# settings.py

Note: Remember that this middleware requires authentication, so it should be loaded after the django.contrib.auth.middleware.AuthenticationMiddleware!

Customization and Settings

You can customize a specific Cache-control value on each URL. For example if we want to avoid cache on /hello/ , but always have it on /api/search for 5 minutes, we should write this in our settings file:

# settings.py
    (r'/hello/$', 'private', 0),
    (r'/api/search$', 'public', 300),

Other options are available to customize the behaviour of the middleware:

SCC_VARY_HEADERS: A list of strings specifying which headers to includ in the Vary header. Default value: ['Accept-Encoding', 'Accept-Language', 'Cookie']

SCC_SET_EXPIRE_HEADER: Define if the middleware should set the Expires header. Default value: True

SCC_MAX_AGE_PUBLIC: Define the default max-age value in seconds for public requests. Default value: 86400

SCC_MAX_AGE_PRIVATE: Define the default max-age value in seconds for private requests. Default value: 0

SCC_DISABLED: Disable the addition of headers, such as during development. Default value: False