Django HTML Minifier

0.11.0 · maintenance · verified Mon Apr 13

django-htmlmin is an HTML minifier designed for Python web frameworks, primarily Django. It provides middleware and utility functions to reduce HTML size, improving page load times. The current version is 0.11.0, released in March 2019, and while functional, it is considered less actively maintained than some alternatives.

Warnings

Install

Imports

Quickstart

To enable HTML minification, add `htmlmin` to `INSTALLED_APPS` and include `MarkRequestMiddleware` and `HtmlMinifyMiddleware` in your `MIDDLEWARE` setting. Proper ordering with Django's caching middleware is crucial. You can also control minification via the `HTML_MINIFY` setting or exclude specific URLs with `EXCLUDE_FROM_MINIFYING`.

# settings.py
INSTALLED_APPS = [
    # ...
    'htmlmin',
    # ...
]

MIDDLEWARE = [
    # ... other middlewares (e.g., GZipMiddleware, UpdateCacheMiddleware)
    'htmlmin.middleware.MarkRequestMiddleware',
    'htmlmin.middleware.HtmlMinifyMiddleware',
    # ... other middlewares (e.g., FetchFromCacheMiddleware, DebugToolbarMiddleware)
]

# Optional: Enable minification in DEBUG mode
# HTML_MINIFY = True

# Optional: Exclude specific URL patterns from minification
# EXCLUDE_FROM_MINIFYING = ('^/admin/', '^/api/')

# views.py example for decorator
from django.shortcuts import render
from htmlmin.decorators import minified_response

@minified_response
def my_minified_view(request):
    return render(request, 'my_template.html', {'data': 'Some content'})

view raw JSON →