3

This is my function:

function convert_strings() {
var chart_labels = {{ chartlabels }};
var array_length = chart_labels.length;
for (var i = 0; i < array_length; i++) {
    chart_labels[i] = chart_labels[i].replace(/&#039;/g, "'")
    }
return chart_labels
}

This is my error: "Uncaught SyntaxError: Unexpected token &"

function convert_strings() {
var chart_labels = [&#39;CHENNAI LPG RO&#39;, &#39;KOCHI LPG RO&#39;, &#39;BANGALORE LPG RO&#39;, &#39;HUBLI LPG RO&#39;, &#39;MADURAI LPG RO&#39;, &#39;MANGLORE LPG RO&#39;];
var array_length = chart_labels.length;
for (var i = 0; i < array_length; i++) {
    chart_labels[i] = chart_labels[i].replace(/&#039;/g, "'")
    }
return chart_labels
}

Please advice what to do :)

6
  • The declaration of your chart_labels array isn't valid. You're missing quotes. Commented Aug 1, 2018 at 7:01
  • @Zenoo: I think these were added, but escaped :). The data should be turned into a non-escaped JSON blob. Commented Aug 1, 2018 at 7:02
  • @WillemVanOnsem It doesn't make much sense, since he's trying to remove those escaped quotes in the next RegEx. Commented Aug 1, 2018 at 7:05
  • @Zenoo: that is exactly to remove the escaping, but since it is not escaping in a string, of course it does not work. But trying to rewrite code literals with string processing, is unfortunately something one sees often here :(. Commented Aug 1, 2018 at 7:09
  • @WillemVanOnsem how to convert my data into non-escaped JSON blobs. I want my data to be an array of strings. Commented Aug 1, 2018 at 7:11

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This works when the script is included in the html, but it doesn't work when the javascript file is external adn being loaded with the Media class. Instead, return {{ chartlabels_json|safe }}; results in a syntax error on the second "{". And when I do return "{{ chartlabels_json|safe }}";, I end up with that exact string. So, got any solutions for external javascript?
@JoshuaSwain: this will of course not work, since Django does not render static files (in production, it does not even serve static files either), and Django only works at the backend so the template tags do not "work" at the frontend. You should make the external script in such way that it accepts a parameter with the JSON object.
2

{{ chart_labels|safe }}

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

Comments

0

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

var str = '&#39;CHENNAI LPG RO&#39;'
console.log(str.replace(/\&\#39;/g, "'"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.