April 10th, 2017

BeDjango: How To Adaptate Cookies To EU Standards In Django Projects

Programing, Python, by admin.

We everyone know what a cookie is; that small portion of data saved locally on the browser that we’ve might used it twice in development environments.

But, once in production, we must know that the European Comission (EU) have strong policies about the use of cookies. You can head towards http://ec.europa.eu/ipg/basics/legal/cookies/index_en.htm to get a deeper knowledge. To make it short, you cannot use cookies freely. What’s more, the EU also defines two types of cookies, the persistent cookies (the cookies that remains on your local machine for a period of time) or the session cookies (the ones that expire once you close the browser/session).

There are many other restrictions, too. For example;  our website needs a clear banner, disposed on 24 languages, that must inform the user that the site can, and will, store cookies.

So, let’s enter in our study case: we’re in a project we’ve used many cookies. How can we adapt them to the EU standards? A django library has the perfect solution: Django Cookielaw. You can check the GitHub repo right here: https://github.com/TyMaszWeb/django-cookie-law

To sum up: it’s a library that adapts the use of our cookies as the EU standards tell. There are two previous requisites to install it:

  1. Having a Django version superior to 1.2
  2. Having the django-classy-tags library installed, and superior to 0.3.0.

Once we get the previous steps done, it’s as easy as pie to use; follow our steps!

Firstly, we should install it via pip repository:

pip install django-cookie-law

Secondly, as every library we install, we must tell the Django application that is installed. And, of course, is the same way as ever; we go to the settings.py located in our enviroment and, in the INSTALLED_APPS area, type this in: cookielaw

In the same file, you should check the TEMPLATE_CONTEXT_PROCESSORS part and see if this line exists


If it doesn’t, add it.

Thirdly, and this is important; now you must run python manage.py collectstatic to copy all the static content of your installed app in your media folder.

Once the project knows that cookielaw is present, we head towards the static folder. The next step is to add the cookielaw.js file located on the GitHub repository in our folder, following an specific route: cookielaw/js/cookielaw.js

We can do it manually or via django-pipeline.

To finish the installation, you have to add a new template; cookielaw/banner.html in your templates directory.

Once this is done, it’s over. Easy, right? Well, we’re moving onto summoning it and giving it some nice HTML structure.

To summon the banner in the page/template you want, you’ll have to use two templatetags; {% load cookielaw_tags%}  to load the necessary stuff, and {% cookielaw_banner %} to load the banner that tells the user about the cookies.

It must be said that, by default, comes without a proper style. Nevertheless, there are some practical examples that we can use located in the repo, but, same as before, they have to follow the specific routes.

For example, we’ve used this structure in our projects,

{% load i18n static %}
<script src="{% static 'cookielaw/js/cookielaw.js' %}"></script>
<div id="CookielawBanner">
<div class="cookie-content">
    <div class="cookie-text">
        {% trans 'This website uses cookies to make your experience better.' %}
    <div class="cookie-buttons">
         <a class="btn-blue" href="{% url 'legal_advice' %}">{% trans "What are the cookies?" %}</a>
         <a class="btn-orange" href="javascript:Cookielaw.createCookielawCookie();">{% trans "Ok, understood" %}</a>
    <div class="close-cookies"></div>
</div> <script type="application/javascript">
$(document).ready(function () {
   $('.close-cookies').click(function () {

Don’t be afraid a give it a shot: share with us the results of your success. Have a great time trying!

Back Top

Leave a Reply