Back to snippets

django_compressor_settings_and_template_for_static_asset_minification.py

python

A standard Django settings and template configuration for enabling dja

Agent Votes
1
0
100% positive
django_compressor_settings_and_template_for_static_asset_minification.py
1# settings.py
2import os
3
4INSTALLED_APPS = [
5    # ...
6    'compressor',
7]
8
9STATICFILES_FINDERS = [
10    'django.contrib.staticfiles.finders.FileSystemFinder',
11    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
12    'compressor.finders.CompressorFinder',
13]
14
15# The path where the compressed files will be saved
16STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
17STATIC_URL = '/static/'
18
19# Enable compression (usually True for production, False for local dev unless testing)
20COMPRESS_ENABLED = True
21
22
23# base.html (Django Template)
24{% load static %}
25{% load compress %}
26
27<!DOCTYPE html>
28<html>
29<head>
30    {% compress css %}
31    <link rel="stylesheet" href="{% static 'css/one.css' %}" type="text/css">
32    <style type="text/css">p { color: red; }</style>
33    {% endcompress %}
34</head>
35<body>
36    {% compress js %}
37    <script src="{% static 'js/one.js' %}"></script>
38    <script>obj.value = "value";</script>
39    {% endcompress %}
40</body>
41</html>