Registry / web-framework / django-htmlmin

django-htmlmin

JSON →
library0.11.0pypypi✓ verified 23d ago

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.

pip install django-htmlmin
INSTALL
IMPORT
SIG · DJANGO-HTMLMIN
D
django-htmlmin
web-frameworkpythonv0.11.0
Install
2.8s avg
Import
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 21.9MB
glibc
py 3.103.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.
fix
Ensure 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.
fix
Consult 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`).
fix
For 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.
fix
Add `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.
Agent activity
3 hits · last 30 days
node
2
Resources