Skip to content

Instantly share code, notes, and snippets.

@b1
Created July 21, 2012 11:02
Show Gist options
  • Save b1/3155460 to your computer and use it in GitHub Desktop.
Save b1/3155460 to your computer and use it in GitHub Desktop.
Django middleware spaceless
# 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