Skip to main content

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.

Release files for django-csp-helpers 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-csp-helpers 1.0.4
File Size Uploaded
django_csp_helpers-1.0.4.tar.gz 11.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-csp-helpers 1.0.4
File Interpreter ABI Platform
django_csp_helpers-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 19.0 kB

Release files / django_csp_helpers-1.0.4.tar.gz

Download URL django_csp_helpers-1.0.4.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0e538b5ade329f9357811c0e48fe2b068d8832b9bc83b9c7b774c12cb941865a
BLAKE2b-256 checksum
How to use checksums
17ad7955d16becdf97e24eb8e1342bbcae71f87b99d0c425453d13081f9db89b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2026.

Transparency log

Release files / django_csp_helpers-1.0.4-py3-none-any.whl

Download URL django_csp_helpers-1.0.4-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b61a6bbc3f8bcf8c5f29f6963366560205891677283c5470eb64c4ba076763a1
BLAKE2b-256 checksum
How to use checksums
7f45f318054060369e3b2e3b3b39c947f71114d2eab2d5ef8cc8ec7c64647a00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page