django-pipeline-csp
CSP nonce support for django-pipeline script tags.
Under a nonce-based Content Security Policy with 'strict-dynamic', browsers ignore host allowlists, so every
<script> needs the request's nonce. django-pipeline renders its tags without one
(jazzband/django-pipeline#771). django-pipeline-csp provides a
drop-in {% javascript %} tag that adds it.
Installation
pip install django-pipeline-csp
INSTALLED_APPS = [
# ...
"pipeline",
"pipeline_csp",
]
Usage
Replace {% load pipeline %} with {% load pipeline_csp %}:
{% load pipeline_csp %}
{% stylesheet "base" %}
{% javascript "polyfills" %}
Every <script> rendered by {% javascript %} gets nonce="..." — individual source files
(PIPELINE_ENABLED = False), the compressed bundle and inline JavaScript templates alike. {% stylesheet %} is
passed through unchanged, so one load is enough.
Do not load both libraries in one template: the library loaded last wins, and with
{% load pipeline_csp pipeline %} the nonce is silently missing.
Nonce sources
The nonce is taken from, in this order:
- Django's built-in CSP (Django >= 6.0):
django.middleware.csp.ContentSecurityPolicyMiddleware - django-csp >= 4.0:
csp.middleware.CSPMiddleware(pip install django-pipeline-csp[django-csp])
Your policy must include the nonce in script-src, e.g. CSP.NONCE (Django) or csp.constants.NONCE (django-csp).
Without an active middleware the output is identical to django-pipeline's.
request must be in the template context (django.template.context_processors.request).
The tag must render before the CSP middleware writes the response header — normal template rendering already
satisfies this. Accessing the nonce after the header was written raises CSPNonceError with django-csp; with
Django's built-in CSP a late nonce is simply not included in the header and the scripts it was meant to allow are
blocked. This matters mainly for streaming responses, where content can be produced after the headers are sent.
Not covered
- Jinja2 templates
- Nonces on
<link rel="stylesheet"> - Scripts injected via
document.write('strict-dynamic'does not trust parser-inserted scripts) - Inline event handlers such as
onclick="..."
Compatibility
Python 3.10–3.14, Django 4.2 / 5.0 / 5.1 / 5.2 / 6.0 / 6.1, django-pipeline >= 4.1.
License
MIT
Release files for django-pipeline-csp 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_pipeline_csp-0.1.0.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_pipeline_csp-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.6 kB
Release files / django_pipeline_csp-0.1.0.tar.gz
| Download URL | django_pipeline_csp-0.1.0.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
63a0039f453edec5a5f9cbe7e217c5c452a50652d5728965d8d5bb73debf2510
|
|
BLAKE2b-256 checksum How to use checksums |
2b7f942aa31157a68031000ca4d9d27b35861c8cab29c89037a228920916ec22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 17, 2026.
Transparency logRelease files / django_pipeline_csp-0.1.0-py3-none-any.whl
| Download URL | django_pipeline_csp-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
00454df902fe7954ebc1e2315b49ef550095c7b05dc5089765a55d9a93437706
|
|
BLAKE2b-256 checksum How to use checksums |
6c2623f6fe988dff84305d93884d1a920382e564124554077bae2c72f65baab5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 17, 2026.
Transparency log