Created
July 21, 2012 11:02
-
-
Save b1/3155460 to your computer and use it in GitHub Desktop.
Django middleware spaceless
This file contains 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
# coding=utf-8 | |
#!/usr/bin/env python | |
""" spaceless middleware | |
MIDDLEWARE_CLASSES += ('core_utils.middleware.SpacelessMiddleware',) | |
""" | |
from django.conf import settings | |
from django.utils.html import strip_spaces_between_tags | |
import re | |
class SpacelessMiddleware(object): | |
""" trim spaces between tags if not in DEBUG """ | |
def process_response(self, request, response): | |
if not settings.DEBUG: | |
cont = response.content | |
cont = strip_spaces_between_tags(cont) | |
response.content = re.sub(r'^\s+<','<', cont ) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment