[Solved]-Compacting/minifying dynamic html


For a bit of background, this is done intentionally, since Django’s templating engine currently doesn’t care about sanitizing lines that evaluate to blanks after the tags get stripped out and doing so will incur some minor performance penalties in serving the response, since it involves a post-processing step that evaluates the full contents of the rendered template.

Still, if you need a quick solution, I suggest you employ the StripWhitespaceMiddleware response middleware that’ll remove spurious whitespaces from textual responses. It’s pretty fast and straightforward, employing regular expression matching like the templating engine itself. A more taxing, but powerful alternative would be deploy a response middleware that utilizes an HTML prettifier, if you really care about the poor human beings still reading raw responses.


django-htmlmin package is a awesome, it provides a lot of control and features moreover it supports HTML 5.


Looking around I just found django-pipeline, I just tried, by now, the middleware and it’s just what I’m expected. It minify all html and text nodes and keep javascript nodes untouched.

Middleware implementation and basic test result is about 4 minutes, just pip install and activate the middleware after you gzip middleware or on top of your MIDDLEWARE_CLASSES setting.




I think a front-end development tool, such as grunt-contrib-htmlmin, should be used to minify Django template files before deploying them to production server.

This way, the minification process happens in “build-time”, and saves server CPU resource for HTML minification in “run-time”.

This tool is originally designed for minifying HTML, but also works for Django templates as I tried out.


If you’re using with Nginx its Strip module is doing really great job with cleaning the html for you. It’s real time and transparent. You don’t need to care about at all after you set it up correctly.



“I think a front-end development tool, such as grunt-contrib-htmlmin, should be used to minify Django template files before deploying them to production server.”

I had a need to do this some time ago to increase performance and decrease latency. I wrote a template minimizer that will minimize the HTML, CSS, and Javascript in your templates beforehand so that the pages are composed minimized. It integrates into Django as a Django command, understands the intricacies of the Django tempting language, and is extendable. You can find it on pypi at the link below:



I recommend using a custom template loader django-spaceless-templates, which strips unnecessary whitespace characters. This approach has two advantages:

  • With the combination with cached template loader, whitespace stripping is done only once during template compilation. On the other hand with {% spaceless %} or middleware approach minification is called every time a template is rendered.
  • Whitespace stripping can be more aggressive – remove spaces between HTML tags, Django tags and variables.

Leave a comment