Last active
July 3, 2016 18:05
-
-
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.
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
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