{"id":5188,"library":"django-htmlmin","title":"Django HTML Minifier","description":"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.","status":"maintenance","version":"0.11.0","language":"en","source_language":"en","source_url":"https://github.com/cobrateam/django-htmlmin","tags":["django","html","minification","performance","middleware"],"install":[{"cmd":"pip install django-htmlmin","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Framework integration for middleware and decorator usage.","package":"Django","optional":false}],"imports":[{"symbol":"HtmlMinifyMiddleware","correct":"from htmlmin.middleware import HtmlMinifyMiddleware"},{"note":"Older versions might have used different naming conventions, but 'MarkRequestMiddleware' is correct for 0.11.0.","wrong":"from htmlmin.middleware import RequestMinifyMiddleware","symbol":"MarkRequestMiddleware","correct":"from htmlmin.middleware import MarkRequestMiddleware"},{"note":"The `htmlmin` module itself doesn't directly expose a `minify` function at its top level; the function is within `htmlmin.minify`.","wrong":"import htmlmin; htmlmin.minify()","symbol":"html_minify","correct":"from htmlmin.minify import html_minify"},{"symbol":"minified_response","correct":"from htmlmin.decorators import minified_response"}],"quickstart":{"code":"# settings.py\nINSTALLED_APPS = [\n    # ...\n    'htmlmin',\n    # ...\n]\n\nMIDDLEWARE = [\n    # ... other middlewares (e.g., GZipMiddleware, UpdateCacheMiddleware)\n    'htmlmin.middleware.MarkRequestMiddleware',\n    'htmlmin.middleware.HtmlMinifyMiddleware',\n    # ... other middlewares (e.g., FetchFromCacheMiddleware, DebugToolbarMiddleware)\n]\n\n# Optional: Enable minification in DEBUG mode\n# HTML_MINIFY = True\n\n# Optional: Exclude specific URL patterns from minification\n# EXCLUDE_FROM_MINIFYING = ('^/admin/', '^/api/')\n\n# views.py example for decorator\nfrom django.shortcuts import render\nfrom htmlmin.decorators import minified_response\n\n@minified_response\ndef my_minified_view(request):\n    return render(request, 'my_template.html', {'data': 'Some content'})","lang":"python","description":"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`."},"warnings":[{"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.","message":"There 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consult the official documentation for the precise ordering, typically: `UpdateCacheMiddleware` -> `HtmlMinifyMiddleware` -> `GZipMiddleware` (if used) -> other middlewares -> `FetchFromCacheMiddleware` -> `MarkRequestMiddleware`.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For new projects or if performance is critical, consider evaluating `django-minify-html` which uses a Rust-based minifier for significantly improved speed.","message":"The `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`).","severity":"deprecated","affected_versions":"0.11.0 and earlier"},{"fix":"Add `HTML_MINIFY = True` to `settings.py` if you wish to see minified HTML in development environments.","message":"By 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}