Package: django-htmlmin-ajax
django-html is an HTML minifier for Python, with full support for HTML 5. It
supports Django, Flask and many other Python web frameworks. It also provides a
command line tool, that can be used for static websites or deployment scripts.Why minify HTML code?One of the important points on client side optimization is to minify HTML. With
minified HTML code, you reduce the size of the data transferred from the server
to the client, which results in faster load times.InstallingTo install django-htmlmin, run this on the terminal: :$[sudo]pipinstalldjango-htmlminUsing the middlewareAll you need to do is add two middlewares to yourMIDDLEWARE_CLASSESand
enable theHTML_MINIFYsetting:MIDDLEWARE_CLASSES=(# other middleware classes'htmlmin.middleware.HtmlMinifyMiddleware','htmlmin.middleware.MarkRequestMiddleware',)Note that if you’re using Django’s caching middleware,MarkRequestMiddlewareshould go afterFetchFromCacheMiddleware, andHtmlMinifyMiddlewareshould go afterUpdateCacheMiddleware:MIDDLEWARE_CLASSES=('django.middleware.cache.UpdateCacheMiddleware','htmlmin.middleware.HtmlMinifyMiddleware',# other middleware classes'django.middleware.cache.FetchFromCacheMiddleware','htmlmin.middleware.MarkRequestMiddleware',)You can optionally specify theHTML_MINIFYsetting:HTML_MINIFY=TrueThe default value for theHTML_MINIFYsetting isnot DEBUG. You only
need to set it toTrueif you want to minify your HTML code whenDEBUGis enabled.Excluding some URLsIf you don’t want to minify all views in your app and it’s under a/my_appURL, you can tell the middleware to not minify the response of your views by
adding aEXCLUDE_FROM_MINIFYINGsetting on your settings.py:EXCLUDE_FROM_MINIFYING=('^my_app/','^admin/')Regex patterns are used 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 commentsThe default behaviour of the middleware is to remove all HTML comments. If you
want to keep the comments, set the settingKEEP_COMMENTS_ON_MINIFYINGtoTrue:KEEP_COMMENTS_ON_MINIFYING=TrueUsing the decoratordjango-htmlmin also provides a decorator, that you can use only on views you
want to minify the response:fromhtmlmin.decoratorsimportminified_response@minified_responsedefhome(request):returnrender_to_response('home.html')Decorator to avoid response to be minifiedYou can use thenot_minified_responsedecorator on views if you want to
avoid the minification of any specific response, without using theEXCLUDE_FROM_MINIFYINGsetting:fromhtmlmin.decoratorsimportnot_minified_response@not_minified_responsedefhome(request):returnrender_to_response('home.html')Using thehtml_minifyfunctionIf you are not working with Django, you can invoke thehtml_minifyfunction
manually:fromhtmlmin.minifyimporthtml_minifyhtml='<html>    <body>Hello world</body>    </html>'minified_html=html_minify(html)Here is an example with aFlaskview:fromflaskimportFlaskfromhtmlmin.minifyimporthtml_minifyapp=Flask(__name__)@app.route('/')defhome():rendered_html=render_template('home.html')returnhtml_minify(rendered_html)Keeping commentsBy default,html_minify()removes all comments. If you want to keep them,
you can passignore_comments=False:fromhtmlmin.minifyimporthtml_minifyhtml='<html>  <body>Hello world<!-- comment to keep --></body>  </html>'minified_html=html_minify(html,ignore_comments=False)Using command line toolIf you are not even using Python, you can use thepyminifycommand line
tool to minify HTML files:$pyminifyindex.html>index_minified.htmlYou can also keep the comments, if you want:$pyminify--keep-commentsindex.html>index_minified_with_comments.htmldevelopmentSource hosted atGitHubReport issues onGitHub IssuesPull requests are very welcome! Make sure your patches are well tested.Running testsIf you are using a virtualenv, all you need to do is:$maketestcommunityIRC channel#cobrateamchannel onirc.freenode.netChangelogYou can see the complete changelog on theGithub releases page.LICENSEUnless otherwise noted, thedjango-htmlminsource files are distributed
under the BSD-style license found in the LICENSE file.