Add Hash to Static Files Names in Django

Add Hash to Static Files Names in Django

August 16, 2022
Configuration
static

It is very useful to append hash at the end of static files name such as js/css to prevent situation when website visitors after update still have old version of the file in browser cache.

Django have built-in tool which automatically add hash to the all static files of Django project on python manage.py collectstatic

You need just to add this line to the settings.py file

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

That’s all.

Pay attention, you need to use template tag {% static 'main.css' %} in the Django templates to make it works, this way static files will be loaded from URL like this https://example.com/static/main.1519f9b0b991.css

Comments