Getting Started
How to Install Django GDPR Cookie Consent
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.2
file:./private_wheels/django_gdpr_cookie_consent-3.2.1-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-3.2.1-py2.py3-none-any.whl
2. Add the app to INSTALLED_APPS of your project settings
INSTALLED_APPS = [
# …
"gdpr_cookie_consent",
# …
]
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>
5. Copy, paste, and modify the cookie consent configuration
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).
6. 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
Strictly necessary sections must not have conditional templates.
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.
7. Check if your setup is correct
Check the correctness of your configuration with the following:
(venv)$ python manage.py check gdpr_cookie_consent
You will get errors about misconfigurations, for example:
(gdpr_cookie_consent.E009) Section "essential" must have at least one provider.
If your intention is to keep the configuration incomplete and Django GDPR Cookie Consent works as is, you can silence the errors by adding them to SILENCED_SYSTEM_CHECKS
in the settings, e.g.:
SILENCED_SYSTEM_CHECKS = [
"gdpr_cookie_consent.E009",
]
Here is an overview of all errors with generalized descriptions:
- gdpr_cookie_consent.E001:
COOKIE_CONSENT_SETTINGS
is not defined in the settings. - gdpr_cookie_consent.E002:
["base_template_name"]
is not defined inCOOKIE_CONSENT_SETTINGS
. - gdpr_cookie_consent.E003: Template defined in
["*_template_name"]
doesn't exist. - gdpr_cookie_consent.E004: You cannot set both,
["*"]
and["*_template_name"]
. - gdpr_cookie_consent.E005:
["dialog_position"]
must be one of"center"
,"top"
,"left"
,"right"
,"bottom"
. - gdpr_cookie_consent.E006:
["sections"]
must contain at least one section. - gdpr_cookie_consent.E007: Each section must have a
["slug"]
defined. - gdpr_cookie_consent.E008: Slugs for sections must be unique.
- gdpr_cookie_consent.E009:
["providers"]
for each section must contain at least one provider. - gdpr_cookie_consent.E010:
["cookies"]
for each provider must contain at least one cookie.
8. Translate your titles and descriptions
If your website has more than one language, prepare the translations:
- Use management command
makemessages
to collect translatable strings intodjango.po
files. - Translate the strings to the languages you need.
- Then, use the management command
compilemessages
to compile them todjango.mo
files.