2

I have the following template:

var url = {{url}};
$.getJSON(url...

and the following view:

return render_to_response('template.html', {"url":"/this/url/"})

but for some reason javascript does not treat this as a string. Is there a reason why? What is the syntax that I should be using?

2
  • 2
    What does the JavaScript source look like after the template has been parsed? Commented Aug 19, 2011 at 2:53
  • '{{url}}' as stated by everyone else. A little piece of advice, it is never a good idea to get in the habit of having dynamically written JS. Better to write all objects to a single JSON object and then reference in inside your Javascript - much easier to maintain as it keeps all your dynamic objects in one place. Commented Aug 19, 2011 at 3:04

1 Answer 1

3

This line in your template:

var url = {{url}};

Will become this:

var url = /this/url;

There are no quotes in the template, and there are no quotes in the string, so there are no quotes in the output. You should use this:

var url = "{{url}}";

or even better:

var url = "{{url|escapejs}}";

so that special characters will be treated properly.

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

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.