django-sri
raw JSON → 0.8.0 verified Fri May 01 auth: no python
Subresource Integrity (SRI) for Django. Generates integrity hashes for static files in templates. Current version: 0.8.0. Release cadence: infrequent, last release Oct 2024.
pip install django-sri Common errors
error ModuleNotFoundError: No module named 'django_sri' ↓
cause Library was renamed from django_sri to sri in version 0.8.0.
fix
Change all imports to use 'sri' (e.g., from sri.storage import ...).
error AttributeError: module 'sri' has no attribute 'templatetags' ↓
cause Typical if you try to import sri.templatetags before app is ready or without loading the templatetag module.
fix
In templates, use {% load sri %} before using the sri filter. Do not import sri.templatetags directly.
Warnings
breaking In version 0.8.0, the library module was renamed from django_sri to sri. Imports using 'from django_sri...' will break. ↓
fix Change all imports from django_sri to sri.
gotcha The SRI hash is computed at deploy time (collectstatic) on the static file content. If files change after collectstatic, the hash will mismatch. ↓
fix Run collectstatic after every static file change.
deprecated The old setting 'SRI_ALGORITHM' is deprecated in favor of 'SRI_ALGORITHMS' (list) in version 0.8.0. ↓
fix Replace SRI_ALGORITHM = 'sha384' with SRI_ALGORITHMS = ['sha384'].
Imports
- SRIStaticFilesStorage wrong
from django_sri.storage import SRIStaticFilesStoragecorrectfrom sri.storage import SRIStaticFilesStorage - SriTag
from sri.templatetags.sri import SriTag
Quickstart
# settings.py
INSTALLED_APPS = [
...,
'sri',
]
STATICFILES_STORAGE = 'sri.storage.SRIStaticFilesStorage'
# In template
{% load sri %}
<script src="{% static 'js/app.js' %}" {{ 'js/app.js'|sri }}></script>