Skip to content

Instantly share code, notes, and snippets.

@maxpoletaev
Created September 7, 2014 22:35
Show Gist options
  • Save maxpoletaev/8dd19d2b8cebfd70e566 to your computer and use it in GitHub Desktop.
Save maxpoletaev/8dd19d2b8cebfd70e566 to your computer and use it in GitHub Desktop.
Compress HTML to one line in Django (python3)
import re
class CompressHtmlMiddleware(object):
def __init__(self):
self.whitespace = re.compile(r'^\s+', re.MULTILINE)
self.linebreak = re.compile(r'\n', re.MULTILINE)
def process_response(self, request, response):
if "Content-Type" in response:
if "text" in response['Content-Type']:
response.content = self.whitespace.sub('', response.content.decode())
response.content = self.linebreak.sub('', response.content.decode())
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment