Install & Compatibility
Where this runs
tested against v0.11.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 21.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 2.8s · import 0.000s · 22MB
20MB installed
● package 20MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
HtmlMinifyMiddleware
✓ from htmlmin.middleware import HtmlMinifyMiddleware
✗ from htmlmin.middleware import HtmlMinifyMiddleware
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'})
Debug
Known issues
gotchaThere is a separate, unrelated package named `htmlmin` on PyPI which is a standalone HTML minifier. Importing `htmlmin.minify.html_minify` from `django-htmlmin` will work, but be aware of the naming conflict if you attempt to use both libraries or refer to external `htmlmin` documentation.fixEnsure you are importing from `htmlmin.middleware` or `htmlmin.minify` as part of the `django-htmlmin` package, not the standalone `htmlmin` package if you intend to use the Django-integrated features.
affects: All versions
gotchaThe order of middleware is critical. `MarkRequestMiddleware` should be placed after `FetchFromCacheMiddleware`, and `HtmlMinifyMiddleware` should be placed after `UpdateCacheMiddleware` if Django's caching middleware is used. Placing minification middleware incorrectly can lead to unminified output or errors.fixConsult the official documentation for the precise ordering, typically: `UpdateCacheMiddleware` -> `HtmlMinifyMiddleware` -> `GZipMiddleware` (if used) -> other middlewares -> `FetchFromCacheMiddleware` -> `MarkRequestMiddleware`.
affects: All versions
deprecatedThe `django-htmlmin` project has not seen updates since March 2019. While functional, it is not actively maintained, and faster, more actively developed alternatives exist (e.g., `django-minify-html`).fixFor new projects or if performance is critical, consider evaluating `django-minify-html` which uses a Rust-based minifier for significantly improved speed.
affects: 0.11.0 and earlier
gotchaBy default, `django-htmlmin` only minifies HTML responses when Django's `DEBUG` setting is `False`. To enable minification during development (when `DEBUG` is `True`), you must explicitly set `HTML_MINIFY = True` in your Django settings.fixAdd `HTML_MINIFY = True` to `settings.py` if you wish to see minified HTML in development environments.
affects: All versions
Upgrade
Version history
0.11.0latest on PyPI · released Mar 18, 2019
Audit
Dependencies
DjangorequiredFramework integration for middleware and decorator usage.