Getting Started

How to Install

1. Download and install the package with pip

Get Django GDPR Cookie Consent from Gumroad.

Create a directory private_wheels/ in your project's repository and add the wheel file of the app there.

Link to this file in your requirements.txt:

Django==5.0
file:./private_wheels/django_gdpr_cookie_consent-2.2.0-py2.py3-none-any.whl

Install the pip requirements from the requirements.txt file into your project's virtual environment:

(venv)$ pip install -r requirements.txt

Alternatively to start quickly, install the wheel file into your Django project's virtual environment right from the shell:

(venv)$ pip install /path/to/django_gdpr_cookie_consent-2.2.0-py2.py3-none-any.whl

2. Add the app to INSTALLED_APPS of your project settings

INSTALLED_APPS = [
    # …
    "gdpr_cookie_consent.apps.GdprCookieConsentConfig",
    # …
]

3. Add path to urlpatterns

from django.urls import path, include

urlpatterns = [
    # …
    path(
        "cookies/",
        include("gdpr_cookie_consent.urls", namespace="cookie_consent"),
    ),
    # …
]

4. Include the widget to your template(s)

Load the CSS somewhere in the <head> section:

<link href="{% static 'gdpr-cookie-consent/css/gdpr-cookie-consent.css' %}" rel="stylesheet" />

Include the widget just before the closing </body> tag:

{% include "gdpr_cookie_consent/includes/cookie_consent.html" %}

Link to the cookie management view, for example, in the website's footer:

{% url "cookie_consent:cookies_management" as cookie_management_url %}
<a href="{{ cookie_management_url }}" rel="nofollow">
    {% trans "Manage Cookies" %}
</a>

How to Use

Copy the example of COOKIE_CONSENT_SETTINGS to the end of your project settings, then modify it to match the cookie usage of your website.

In the example above, there are four cookie sections: Essential (strictly necessary), Functionality (optional), Performance (optional), and Marketing (optional).

2. Create templates to render HTML for non-required cookies

Cookies that are set conditionally based upon a visitor’s choices must be rendered from conditional templates.

For example, analytics providers typically instruct to place the analytics tag within the page’s <head> section. However, suppose visitors are being given a choice of whether to accept analytics cookies using Django GDPR Cookie Consent. In that case, the analytics tag that sets the analytics cookies must be rendered from conditional templates.

The conditional HTML might include external or inline styles, external or inline JavaScripts, and other HTML snippets.

Create templates for the snippets that will be loaded or rendered when a particular section is chosen, for example:

  • conditional_html/functionality.html
  • conditional_html/performance.html
  • conditional_html/marketing.html

For testing, you can add the markup like:

<script>
    console.log('Conditional functionality code loaded.');
</script>
<script>
    console.log('Conditional performance code loaded.');
</script>
<script>
    console.log('Conditional marketing code loaded.');
</script>

Manage the scripts that create your Essential (strictly necessary) cookies separately, unrelated to conditional html snippets.

3. Translate your titles and descriptions

If your website has more than one language, prepare the translations:

  • Use management command makemessages to collect translatable strings into django.po files.
  • Translate the strings to the languages you need.
  • Then, use the management command compilemessages to compile them to django.mo files.