Skip to content

Instantly share code, notes, and snippets.

@RutledgePaulV
Last active July 3, 2016 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RutledgePaulV/eca1318b517eca457e4a to your computer and use it in GitHub Desktop.
Save RutledgePaulV/eca1318b517eca457e4a to your computer and use it in GitHub Desktop.
This custom template tag uses the Beautiful Soup library to prettify the html output from rendered django templates.
from django.template import Node
class PrettyPrintNode(Node):
def __init__(self, nodelist):
self.nodelist = nodelist
def render(self, context):
from bs4 import BeautifulSoup
html = BeautifulSoup(self.nodelist.render(context))
return html.prettify()
@register.tag()
def pretty(parser,token):
nodelist = parser.parse(('endpretty',))
parser.delete_first_token()
return PrettyPrintNode(nodelist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment