Skip to main content

A set of template tags (and mixins!) to assist in building CSP-enabled websites.

Project description

django-csp-helpers PyPI

A set of template tags (and mixins!) to assist in building CSP-enabled websites using Django's native CSP support (6.0+) or django-csp.

Install

  1. Add "csp_helpers" to your INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'csp_helpers',
]

Mixins

django-csp-helpers includes a pair of mixins that can be applied to views and forms to allow for the use of CSP nonces in widgets and form media.

How to use

Simply add CSPViewMixin to your Views, and CSPFormMixin to your Forms or ModelForms. You will need to use both mixins together, they don't work alone.

CSPFormMixin

from csp_helpers.mixins import CSPFormMixin

class ArticleForm(CSPFormMixin, ModelForm):
    ...

CSPViewMixin

from csp_helpers.mixins import CSPViewMixin
from .forms import ArticleForm

class ArticleUpdateView(CSPViewMixin, UpdateView):
    form_class = ArticleForm
    ...

Using only CSPFormMixin

If you are managing your form manually, or not using class-based views, you will not be able to use CSPViewMixin. In these cases, just call your form with csp_nonce as an argument manually, like below.

form = ArticleForm(csp_nonce=request.csp_nonce)

What it does

The django-csp-helpers mixins will modify and extend your views and forms in two ways.

Form Widgets

Form widgets will be patched to inject a CSP nonce into the rendering context for template widgets. You can access this with {{ csp_nonce }} in your widget templates.

Form Media

Form media (CSS and JS) will be included with CSP nonces.

Template Tags

django-csp-helpers also includes a pair of template tags

render_bundle_csp

An exact replacement for the django-webpack-loader render_bundle tag that includes bundles with CSP nonces.

{% load render_bundle_csp %}

{% render_bundle_csp 'main' 'css' %}
{% render_bundle_csp 'main' 'js %}

media_csp

A less advanced version of the form media functionality provided by the mixins above. Simply load this tag and pass it a form, and it will include the form media with CSP nonces.

{% load media_csp %}

{# include form media #}
{% media_csp myform %}

License

This software is released under the MIT license.

Copyright (c) 2019-2026 Luke Rogers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_csp_helpers-1.0.2.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_csp_helpers-1.0.2-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file django_csp_helpers-1.0.2.tar.gz.

File metadata

  • Download URL: django_csp_helpers-1.0.2.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_csp_helpers-1.0.2.tar.gz
Algorithm Hash digest
SHA256 82c1e26b38eb42ba375452ca848439f203d630f84e515f7080606162ad69c56d
MD5 ae5ca4253791173dbf2b47ca6129c0b6
BLAKE2b-256 c754186a4873bb46ebec11f2d4f43ff78a7f219fb856498474c8cd7b7fbed55f

See more details on using hashes here.

File details

Details for the file django_csp_helpers-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_csp_helpers-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_csp_helpers-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d378a7046379d51eddd25918db01c7b4a3023616700c572475d2cc9e596b12e4
MD5 f369087355f1c0f33b1514d51f4784eb
BLAKE2b-256 df653cdac27039b19f0ce5428559c488d269046cd43e24a540e1586a5bcc70b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page