Skip to content

Instantly share code, notes, and snippets.

@tarkatronic
Created November 28, 2017 22:10
Show Gist options
  • Save tarkatronic/0b31eef92a74a4b2e2afd2176a5c9088 to your computer and use it in GitHub Desktop.
Save tarkatronic/0b31eef92a74a4b2e2afd2176a5c9088 to your computer and use it in GitHub Desktop.
A Django jsonify template filter
import json
from django import template
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name
register = template.Library()
@register.filter
def jsonify(value):
lexer = get_lexer_by_name('json', stripall=True)
formatter = HtmlFormatter()
return highlight(json.dumps(value, indent=2), lexer, formatter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment