@register.filter
def to_js(value):
    """
    To use a python variable in JS, we call json.dumps to serialize as JSON server-side and reconstruct using
    JSON.parse. The serialized string must be escaped appropriately before dumping into the client-side code.
    """
return mark_safe('JSON.parse("%s")' % escapejs(json.dumps(value, separators
    =(',', ':'))))
Safely Using JSON with html inside of the JSON in Django Templates