[Django]-Uncaught SyntaxError: Unexpected token & in javascript django app

2πŸ‘

βœ…

In the view, you can convert the chartlabels to a JSON blob, for example with:

import json

def some_view(request):
    # ...
    context['chartlabels_json'] = json.dumps(context['chartlabels'])
    # ...
    return render(request, 'some_template.html', context)

In the template, we can then write the blob in an unescaped way:

function convert_strings() {
    return {{ chartlabels_json|safe }};
}

A more convenient way is however probably using the django-jsonify [PyPI] tool, and thus simply pass the charlabel through the jsonify filter.

2πŸ‘

{{ chart_labels|safe }}

This would do it. No need to use .replace or jsonDump

πŸ‘€SachinDesai

0πŸ‘

Use the following, for more details on creating the regex for special characters follows this links javascript regex for special characters:

var str = ''CHENNAI LPG RO''
console.log(str.replace(/\&\#39;/g, "'"));
πŸ‘€Ullas Hunka

Leave a comment