Skip to main content

A powerful HTML whitespace remover for Django

Project description

HTML Whitespace remover for Django Badge PyPi Version Badge PyPI - Python Version PyPI - License Code Style

Introduction :

A powerful tool to optimize Django rendered templates

Why use “django_stip_whitespace” ?

  • Adds line break to InlineJS.
  • It can automagically minify inline CSS, JS.
  • Removes <!–prettier-ignore–> from HTML.
  • It speeds up website by reducing the HTML size.
  • Drop in replacement for django.gzip.GzipMiddleware.
  • It compiles regex at runtime. So it’s blazing fast.
  • Its mostly based on C ( gzip ) and Rust ( minify-html ) libraries.
  • Significantly lower bytes transferred when working with frameworks like AlpineJs ( Almost fully working & Please open a issue in the Issue Tracker if you encounter any bug) & Petite Vue.
  • Is very customizable. ( You can configure lower level minify-html rust bindings and also the lower level python bindings from )

Why souldn’t you use “django_stip_whitespace” ?

  • You don’t like having unnecessary ‘;;’ in your HTML. ( If you know any regex to fix this please put a pull request )
  • Although I tried my best to use Compiled Language for Optimizations. It can still be sub miliseconds ( > 0.001 ) slower compared to normal Django Rendering. ( If you know any way to improve performance, please put a pull request )

Requirements :

  • python-strip-whitespace
  • Django > 3 ( Should work with version 2? )
  • Python 3 ( Should work with all version? )
  • Brotli ( or BrotliPy ) | ( Optional )
  • ZSTD ( Optional )

User guide :

Installation :

Install with pip from pypi (No extra dependencies):

$ python -m pip install django_strip_whitespace

Install with pip with Brotli support:

$ python -m pip install django_strip_whitespace[brotli]

Same but with Zstandard support:

$ python -m pip install django_strip_whitespace[zstd]

Install with pip from github ( Development | Not Recommended for Production ):

$ python -m pip install

Then include it in your django project:


Or if you like:

MIDDLEWARE += "strip_whitespace.middlewares.HtmlStripWhiteSpaceMiddleware.html_strip_whitespace"

Customization :

Change Lower Level Bindings :

Rust :

The module allows rust minifier options to be changed from Django’s file. If you would like to change any settings, refer to minify-html’s source code.

The bindings are ( by default set to True ):

STRIP_WHITESPACE_RUST_DO_NOT_MINIFY_DOCTYPE, # passes do_not_minify_doctype to minify-html
STRIP_WHITESPACE_RUST_ENSURE_SPEC_CONPLIANT_UNQUOTED_ATTRIBUTE_VALUES, # passes ensure_spec_compliant_unquoted_attribute_values to minify-html
STRIP_WHITESPACE_RUST_KEEP_CLOSING_TAGS, # passes keep_closing_tags to minify-html
STRIP_WHITESPACE_RUST_KEEP_COMMENTS, # passes keep_comments to minify-html
STRIP_WHITESPACE_RUST_KEEP_HTML_AND_HEAD_OPENING_TAGS, # passes keep_html_and_head_opening_tags to minify-html
STRIP_WHITESPACE_RUST_KEEP_SPACES_BETWEEN_ATTRIBUTES, # passes keep_spaces_between_attributes to minify-html
STRIP_WHITESPACE_RUST_MINIFY_CSS, # passes minify_css to minify-html
STRIP_WHITESPACE_RUST_MINIFY_JS, # passes minify_js to minify-html
STRIP_WHITESPACE_RUST_REMOVE_BANGS, # passes remove_bangs to minify-html
STRIP_WHITESPACE_RUST_REMOVE_PROCESSING_INSTRUCTIONS, # passes remove_processing_instructions to minify-html

If you would like to change any of the above variables, simply put them in ( Please note that every variable here is a python boolean ).

For example:



Python :

The module allows python minifier options to be changed from Django’s file. If you would like to change any settings, refer to python-module’s source code.

The bindings are ( by default set to a sane value ):

STRIP_WHITESPACE_PYTHON_REMOVE_COMMENTS, # False | removes comments from HTML using python ( not recommended cause rust can do that just fine and fast )
STRIP_WHITESPACE_PYTHON_CONDENSE_STYLE_FROM_HTML, # True | replaces '<style text/css>' -> '<style>'
STRIP_WHITESPACE_PYTHON_CONDENSE_SCRIPT_FROM_HTML, # True | replaces '<script text/javascript>' -> '<script>'
STRIP_WHITESPACE_PYTHON_CLEAN_UNNEEDED_HTML_TAGS, # True | removes some unnecessary tags
STRIP_WHITESPACE_PYTHON_CONDENSE_HTML_WHITESPACE, # True | This is where the magic happens.

If you would like to change any of the above variables, simply put them in ( Please note that every variable here is a python boolean )

For example:



Change Ignored Paths :

This module allows dynamic ignored path allocation. So for example if your sitemap.xml is at url ‘/sitemap.xml’ and you want to avoid minifying it ( Because this module in lower level is meant to minify HTML not XML ). Then you can add it to ignored path. ( By default it ignores ‘/sitemap.xml’ )

To customize ignored path:



Change NBSP Mangle Character :

This module first replaces the &nbsp; character from html with a character. For example &nbsp; becomes ‘অ’ ( I picked ‘অ’ because its a foreign character and not many sites use the character like this ‘অ’ ). If for some reason this character is causing problem in your HTML. You can change this from .

To change &nbsp; mangle character:


# Keep the string as  short as possible.
# If you make it long,
# the python str.replace() method will use more CPU and RAM thus slowing your site down.


Change Compression Settings :

This module can do the work of django.gzip middleware. ( It can also do brotli, zstd 👀 )

To change the compression algorithm ( by default using ‘gzip’ because it’s a python stdlib):

STRIP_WHITESPACE_COMPRESSION_ALGORITHM = "gzip" or "br" or "zstd" or "plain"

To use this module with django.gzip middleware ( or django_brotli middleware ):


Contributing :

If you like this project add a star. If you have problems or suggestions please put them in the Issue Tracker. If you like to add features. Fork this repo and submit a Pull Request. 😛

Updates ?? :

This repository is freezed. It will automatically install latest python-strip-whitespace

Special Thanks to :

  • alfonsrv : For making me realize that this module can be used without django gzip middleware

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Built Distribution

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page