1

So when I am trying to pass a list of country codes to javascript, it does replaces the single quotes -> ' with ' Now I tried to replace all of those with a regular expression, but it would not work. I'm wondering why it will not work to just have those single quotes

function drawRegionsMap() {

        for (item in {{countries}}) {
            item.replace(/'/g, "'");
        }
        var data = google.visualization.arrayToDataTable([
            {{countries}}
        );

In the javascript it's this:

function drawRegionsMap() {

for (item in [['Country'], ['AU'], ['AT'], ['BE'], ['BO'], ... ['LI'], ['MC'], ['ID']]) {
    item.replace(/'/g, "'");
}
var data = google.visualization.arrayToDataTable([
    [['Country'], ['AU'], ['AT'], ['BE'], ['BO'], ['CO'], ... ['ID']]
);

So do I need to pass this Django variable to the javascript a different way? Is this even possible? All help is appreciated

The error: SyntaxError: expected expression, got '&'

5
  • 1
    Have you tried just pasting the raw string instead of the HTML encoded text? Commented Jun 1, 2016 at 19:55
  • 1
    Why don't you do {% for item in countries %}? Commented Jun 1, 2016 at 19:58
  • Because it's already in the right format @ShangWang Commented Jun 1, 2016 at 19:58
  • How are you passing a list of countries to javascript in views.py? Why are you having single quotes? Do you really need single quotes? If so, you could add them manually in javascript code instead of passing them with the country codes. Commented Jun 1, 2016 at 19:59
  • I have found it now! Thanks @MikeC! I had to do {{countries|safe}}, which was provided in the solutions in from that link Commented Jun 1, 2016 at 20:02

2 Answers 2

1

Apparantly this is fixed by doing {{countries|safe}}. I believe that's being used to escape certain characters. Thanks Mike C for providing the link, the solution was found here

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

Comments

0

If safe isn't working try with escapejs it worked for me

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.