2

I am trying to query a list of data, convert it into a json object, and pass it into my javascript so it can be evaluated there:

var data = '{{ passed_list|jsonify }}';

# This evaluates to:

var news = '[{"pk": 133, "model": "Article.article
    ","fields":

However, this wont work because I am trying to access elements.

For example,

var object = data[0].pk;
In my view source, this does not evaluate to 133 as expected but it evaluates
to ... data[0].pk ... which is a bit confusing.

Here is my jsonify:

 if isinstance(object, QuerySet):
    return serialize('json', object)
return simplejson.dumps(object, ensure_ascii=False)

Any help would be appreciated, thanks.

2 Answers 2

1

django will escape html characters by default

if you completely trust the data, (i.e. it comes from your code, and no part of the content could come from a user), you can use

var data = '{{ passed_list|jsonify|safe }}';

to tell django not to escape it

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

4 Comments

Thanks! It looks like that it's almost solved, there is just one more small issue, chrome's debugger is saying there is an "unexpected identifier with the comma betweeen china and s. "title": "China's Toxic Milk Whistleblower Murdered", how should this be handled if I want to escape that quote, but not others?
sounds like a bug in your jsonify.. does it return valid json?
Yes, I actually used django's default snippet for jsonify i updated it in my main post.
Your suggestion worked, I was just being really stupid. '{{ passed_list|jsonify }}' makes no sense, why would I even include those quotes, for some reason, on many online json guides, many people have single quotes on their json objects, not sure why though.
0
var data = '{{ portfolio|jsonify|escapejs|safe }}';

Filter escapejs put after jsonify solves the problem with special characters like: '

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.