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
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.
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'].

Add sri to INSTALLED_APPS, set storage backend, use sri filter in templates

# 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>