Skip to main content

Middleware which sets SameSite flag for session and csrf cookies in legacy versions of Django.

Project description

Django 3.1.x introduces full support of SameSite flag for session and csrf cookie.

Unfortunately, this functionality will not be ported to older versions of Django e.g. 1.11.x, 2.2.x or 3.0.x

This repository contains a middleware which automatically sets SameSite attribute for session and csrf cookies in legacy versions of Django.


Install django-cookies-samesite:

pip install django-cookies-samesite

Add the middleware to the top of MIDDLEWARE_CLASSES:



If you’re planning to use this middleware together with the newer version of Django (>3.0.x), and you want to e.g. set the SameSite attribute to none, then you have to add DCS_ prefix to all django-cookies-samesite settings (see examples below).

It will help you to avoid bugs caused by the conflicting settings names.

Related bug:

Set your preferred SameSite policy in .. code-block:: python


This can be ‘Lax’, ‘None’, ‘Strict’, or None to disable the flag. Also, you can set this flag in your custom cookies:

SESSION_COOKIE_SAMESITE_KEYS = {'my-custom-cookies'}
# or
DCS_SESSION_COOKIE_SAMESITE_KEYS = {'my-custom-cookies'}

After that you should be able to see the SameSite flag set for session and csrf cookies.

You can set the SameSite flag on all cookies (even on those coming from third-party Django apps):

# or

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox


Tools used in rendering this package:


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-cookies-samesite, version 0.8.0
Filename, size File type Python version Upload date Hashes
Filename, size django-cookies-samesite-0.8.0.tar.gz (7.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page