Created
September 7, 2014 22:35
-
-
Save maxpoletaev/8dd19d2b8cebfd70e566 to your computer and use it in GitHub Desktop.
Compress HTML to one line in Django (python3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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