Django Do Not Track

Overview

Build status

Django utilities for honoring the Do Not Track HTTP header.

Mathieu Leplatre posted an article Django: Do not forget Do Not Track. I really appreciated the post, and wanted to implement the ideas in my projects. Being lazy, I only wanted to do the work once, so I took his ideas and packaged them up with some tests and docs.

Included is middleware for detecting HTTP_DNT and passing its information on to both views and templates via the request object, and a useful context processor. The middleware also adds a vary header for cache control.

This package requires Python 2.7 or newer and Django 1.4 or later.

Contents

Getting Started

First, read Mathieu Leplatre’s article Django: Do not forget Do Not Track.

Requirements

django-donottrack requires Python 2.7 or newer and Django 1.4 or newer.

Installation & Usage

Included is middleware for detecting HTTP_DNT and passing its information on to both views and templates via the request object, and a useful context processor. The middleware also adds a vary header for cache control.

Installation of the middleware is required. The context processor is convenient and thus recommended.

Settings:

MIDDLEWARE_CLASSES = (
    # default/other processors ...
    'donottrack.middleware.DoNotTrackMiddleware',
    # default/other processors ...
)

TEMPLATE_CONTEXT_PROCESSORS = (
    # default/other processors ...
    'donottrack.context_processors.donottrack',
)

Then in your template you can do things like:

{% if not donottrack %}
    {% include "google-analyitcs.html" %}
{% endif %}

And your views can also handle DNT:

def my_view(request):
    if not request.donottrack:
        # Log some request data ...

    # continue with view logic

Tip

Adding this app to your INSTALLED_APPS is currently unnecessary unless you want to run tests.

Other Information

Important

Installing this app in your Django project does not mean that you honor Do Not Track any more than installing django-secure means your web applicaiton is secure. It only means you have some tools to help with that end goal. You will need to audit your full stack to ensure that you are honoring DNT. But this app is a great start, and we hope you find it useful.

Note

This is an initial release. Despite being simple and theoretically solid (it has a full test suite) this is a beta-type release, and the public API may change, and as I learn more about DNT, more functionality may be added.

Development

Source Code

donottrack source code is managed using Git, and can be found on GitHub. Feel free to clone, fork, and contribute.

Documentation

The documentation is written in plain text, viewable practially anywhere. An HTML version of the docs can be found online at Read the Docs. If you want to build a local version of these, you can install Sphinx, and then from the doc directory in this repository, run:

make html

You will find the built docs in the docs/_build/html directory.

Tests

Build status

donottrack has a full test suite. Current build status can be found at Travis CI.

Test settings are included, so tests can be run outside of a Django project:

django-admin.py test donottrack --settings="donottrack.tests.settings"

If you have not installed donottrack, but are working from a Git checkout, you will need to either have it on your PYTHONPATH or run the above command from the root of the repository.

Note

In order for donottrack tests to run in your project, you will need to add donottrack to your INSTALLED_APPS. (Mentioned here because no other donottrack functionality requires this.)

Indices and tables