Meta

Maintainers

Project description

django-html in an HTML minifier for Python with full support for HTML 5. It
supports Django, Flask and any other Python web framework. It also provides a
command line tool that can be used for static websites or deployment scripts.

Why minify HTML code?

One of important points on client side optimization is minify HTML, with
minified HTML code, you reduce the size of data transferred from your server to
your client, and your pages load faster.

Installing

For install django-htmlmin, run on terminal:

$ [sudo] pip install django-htmlmin

Using the midleware

All you need to do is add two middlewares to your MIDDLEWARE_CLASSES and
enable the HTML_MINIFY setting:

The default value for the HTML_MINIFY setting is not DEBUG. You only
need to set it to True if you want to minify your HTML code when DEBUG
is enabled.

Excluding some URLs

If you don’t want to minify all views in your app and it’s under a /my_app
URL, you can tell the middleware to not minify the response of your views by
adding a EXCLUDE_FROM_MINIFYING setting on your settings.py:

EXCLUDE_FROM_MINIFYING = ('^my_app/', '^admin/')

As you can see, you use a regex pattern for URL exclusion. If you want to
exclude all URLs of your app, except a specific view, you can use the decorator
minified_response (check the next section above).

Keeping comments

The default behaviour of the middleware is remove all comments from HTML. If
you want to keep your comments, set the setting KEEP_COMMENTS_ON_MINIFYING
to True:

KEEP_COMMENTS_ON_MINIFYING = True

Using the decorator

django-htmlmin also provides a decorator, that you can use only on views you
want to minify the response: