0

So, you want to compress the size of the HTML that Django generates? Here is the Django way to do it.

In order to do this, you’ll have to write a middleware for your application.

So, create a file called middleware.py (for this example, but you can name it whatever you want) inside your Django app folder (let’s call the app MyTestApp). Copy the following code inside it.

import re
from django.utils.html import strip_spaces_between_tags
from django.conf import settings

RE_MULTISPACE = re.compile(r"s{2,}")
RE_NEWLINE = re.compile(r"n")

class MinifyHTMLMiddleware(object):
    def process_response(self, request, response):
        if 'text/html' in response['Content-Type'] and settings.COMPRESS_HTML:
            response.content = strip_spaces_between_tags(response.content.strip())
            response.content = RE_MULTISPACE.sub(" ", response.content)
            response.content = RE_NEWLINE.sub("", response.content)
        return response

What this does is: first, it removes all the spaces between tags. The problem is that it will remove the space only if between the tags there will be ONLY spaces. But sometimes you have cases like this:

<div> .....</div>         Some text    <div> .....</div>

In this case, the space between the two divs will not be compressed because of the “Some text” string, but nevertheless, the spaces will be rendered as only one. To solve this, the next thing it does is replace multiple appearances of spaces with only one space. Finally, it will remove all the new lines from the html, because they are anyway ignored by the browsers.

After adding the above code to middleware.py, add the following line to MIDDLEWARE_CLASSES in your settings file:

'MyTestApp.middleware.MinifyHTMLMiddleware'

Replace MyTestApp with the name of you app and middleware with the name that you gave to the middleware.py file (if you did gave it another name).

Hope this helps!

UPDATE

You also have to add in your settings COMPRESS_HTML and set it to True, if you want to compress the html, or False otherwise.

COMPRESS_HTML = True

Update for new Django 1.10 it need this change.

from django.utils.deprecation import MiddlewareMixin

class MinifyHTMLMiddleware(MiddlewareMixin):
    ....  

Update for Python3 and Django 1.10+

import re
from django.utils.deprecation import MiddlewareMixin
from django.utils.html import strip_spaces_between_tags
from django.conf import settings

RE_MULTISPACE = re.compile(r"s{2,}")
RE_NEWLINE = re.compile(r"n")


class MinifyHTMLMiddleware(MiddlewareMixin):

    def process_response(self, request, response):
        if 'text/html' in response['Content-Type'] and settings.COMPRESS_HTML:
            response.content = strip_spaces_between_tags(response.content.strip())
            response.content = RE_MULTISPACE.sub(" ", response.content.decode('utf-8'))
            response.content = RE_NEWLINE.sub("", response.content.decode('utf-8'))
        return response

Original source: http://www.soyoucode.com/2011/minify-html-output-d

problem solution templates django tricks python

Your Answer

blog comments powered by Disqus