Skip to content

Repository files navigation

Django Cookie Filter

Django middleware which removes all unwanted cookies - useful for improving cache hit ratios when analytics cookies interfere with caching.

Installation

Using pip:

$ pip install django-cookiefilter

Edit your Django project's settings module, and add the middleware to the start of MIDDLEWARE:

MIDDLEWARE = [
    "cookiefilter.middleware.CookieFilterMiddleware",
    # ...
]

Note

The middleware should be added before UpdateCacheMiddleware, as it uses the value of HTTP_COOKIES which needs to be modified.

Configuration

Out of the box the standard Django cookie names will work without any other configuration. However if your project uses different or additional cookie names, edit COOKIEFILTER_ALLOWED_NAMES in your project's settings module:

COOKIEFILTER_ALLOWED_NAMES = [
    "analytics",
    "csrftoken",
    "django_language",
    "messages",
    "sessionid",
]

Note

This setting was previously named COOKIEFILTER_ALLOWED, which is now deprecated and will be removed in a future version.

Or if you need to allow multiple cookies that don't have specific names, you can add COOKIEFILTER_ALLOWED_PATTERNS to allow additional cookie names with regular expressions:

COOKIEFILTER_ALLOWED_PATTERNS = [
    r"^abtesting-\d+-version$",
]

About

Django Cookie Filter

Resources

Stars

1 star

Watchers

6 watching

Forks

Releases

Used by

Contributors

Languages