Skip to content

Instantly share code, notes, and snippets.

@marcus-crane
Created November 18, 2017 10:37
Show Gist options
  • Save marcus-crane/01ad3aeb465130bda57999eaba40842d to your computer and use it in GitHub Desktop.
Save marcus-crane/01ad3aeb465130bda57999eaba40842d to your computer and use it in GitHub Desktop.
Django 2.0 HTML Prettify Middleware
from html5print import HTMLBeautifier
class PrettifyMiddleware:
"""Prettify HTML for anyone interested in reading the source of this site."""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
if response['Content-Type'].split(';')[0] == 'text/html':
response.content = HTMLBeautifier.beautify(response.content, 2)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment