
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
django-privacy-policy-tools
Advanced tools
This is a highly configurable Django app to manage privacy policies and confirmations.
This is a Django app to manage privacy policies and to handle the process of confirmation trough the users.
Features:
This app is build for the current LTS Version of Django, which is 4.2.
Install the app using pip.
pip install django-privacy-policy-tools
At first you have to add the app to your INSTALLED_APPS
in your settings.py
.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'privacy_policy_tools.apps.PrivacyPolicyToolsConfig',
...
)
Add the middleware of the app to your MIDDLEWARE
in your settings.py
.
MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'privacy_policy_tools.middleware.PrivacyPolicyMiddleware',
...
)
Add the URL configuration to your main urls.py:
urlpatterns = [
...
re_path(r'^privacy/', include('privacy_policy_tools.urls')),
...
]
If you want to use the app in your templates (e.g. create a link to
the privacy policy page), you should
add the context processor of the app to your config in your settings.py
.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.request',
'privacy_policy_tools.context_processors.privacy_tools',
],
'debug': True,
},
},
]
Now you can configure the app in your settings.py
.
PRIVACY_POLICY_TOOLS = {
'ENABLED': True,
'POLICY_PAGE_URL': 'terms/and/conditions',
'POLICY_CONFIRM_URL': 'terms/and/conditions/confirm',
'IGNORE_URLS': ['admin', ],
'DEFAULT_POLICY': True
}
Possible values are:
After configuring the app everything is ready to be used. Start by creating a policy in the admin interface. It is possible to create different policies for different groups. Depending on your language settings you will get a field for each language for the policy title and text.
Now the users are required to confirm your created policies.
The included templates are using the base templet of the admin site. If you want
to change this style to match your project you can overwrite these two templates
by placing them in an app loaded in INSTALLED_APPS
before this app. There
are the following templates:
privacy_policy_tools/show.html
privacy_policy_tools/confirm.html
In show.html
you have to place something like this:
{% for policy in policies %}
<h3>{{ policy.title }}</h3>
<p><small>{% translate "Last changed:" %} {{ policy.published_at }}</small></p>
<p>{{ policy.text|safe }}</p>
{% endfor %}
In confirm.html
you have to do something like this:
<h3>{{ policy.title }}</h3>
<p>{% translate "Last changed:" %}
{{ policy.published_at }}</p>
<p>{{ policy.text|safe }}</p>
{% if is_authenticated and not is_confirmed %}
{% if policy.confirm_checkbox is True %}
{% if form.non_field_errors %}
<ul class="errorlist">
<li>{{ form.non_field_errors }}</li>
</ul>
{% endif %}
{% endif %}
<form method="post" action="{{ form_url }}">
{% csrf_token %}
{% if policy.confirm_checkbox is True %}
{% for field in form %}
{% if field.errors %}
<ul class="errorlist">
{% for error in field.errors %}
<li>{{ error|escape }}</li>
{% endfor %}
</ul>
{% endif %}
<div class="checkbox">
{{ field }} {{ field.label }}
</div>
{% endfor %}
{% endif %}
<input type="submit" class="default" id="btn_create"
value="{{ policy.confirm_button_text }}"
style="float: left !important;"/>
</form>
{% endif %}
If you want to place a link to the policies at your page (e.g. in the footer) you have to add a context processor as shown above. After that you can create the link in your template in the following way:
{% if privacy_enabled %}
<a href="{% url privacy_view %}">{% translate "Terms and Conditions" %}</a>
{% endif %}
To add a checkbox to your registration form where the users could confirm your policies during registration, you have to do multiple things. At first create the form and add the fields:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from privacy_policy_tools.utils import get_setting
class UserCreateForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super(UserCreateForm, self).__init__(*args, **kwargs)
if get_setting('ENABLED', False):
self.fields['confirm_privacy'] = forms.BooleanField(
label=_('I accept the Terms and Conditions')
)
In the corresponding view you have to save the confirmation. Such a view could look like this:
from django.shortcuts import render
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from privacy_policy_tools.utils import save_confirmation, get_setting
from yourapp.forms import UserCreateForm
def create_user(request):
if request.method == 'POST':
form = UserCreateForm(request.POST, label_suffix='')
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
email = form.cleaned_data['email']
user = User.objects.create_user(username, email, password)
if get_setting('ENABLED'):
save_confirmation(user)
return HttpResponseRedirect(reverse('yourapp.views.index'))
else:
form = UserCreateForm()
return render(request, 'registration/create_user.html', {
'form': form,
})
It is possible to add a hook at the beginning of the evaluation if the privacy policy should be shown to the user. With this hook you can perform additional checks depending on your project.
To add this hook add the following setting to the configuration in settings.py
:
The app is able to request a second confirmation to a privacy policy. This may be case if a confirmation of a parent is required. For that an email is sent to the person for the second confirmation.
To use this feature some settings must be provided:
To further customize the templates it is possible to override them. For that copy the templates beginning with second_confirm to your project and change it according to your needs.
FAQs
This is a highly configurable Django app to manage privacy policies and confirmations.
We found that django-privacy-policy-tools demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.