Background
I am building an app that helps people search for lifts. I'm using Django, Python2.7 and the Google maps and directions APIs.
In one view I use a map and allow the user to pick a few locations as well as arrival and leaving times. Then I use the Google Directions API to grab a few alternative routes between points (as JSON). I parse the JSON and alter the info returned in a number of ways and save the data in a new structure (called legs
). Up until here everything works fine.
When the user presses the 'next' button then legs
is stringified and POSTed to the next view as part of a form. Like so:
document.getElementById('legs_field').value = JSON.stringify(window.legs);
It is also possibly worth noting that this line works fine:
JSON.parse(JSON.stringify(window.legs));
The Problem
I cant seem to get legs
accessible to the JavaScript in the next view. So my question is really: How do I get the same legs
data structure accessible in the next view?
What I've tried
Approach 1:
view.py
legs_json = request.POST.get('legs')
legs = json.loads(legs_json)
arbitrary_processing()
return render_to_response('foo.html',{
'legs': SafeString(json.dumps(legs)),
**snipped for brevity**
})
template
window.legs_json = '{{legs}}';
**snipped for brevity**
js
window.legs = JSON.parse(window.legs_json);
error on js:
SyntaxError: JSON.parse: bad escaped character
I have used this exact technique when dealing with JSON before and it worked fine. So I assume there is some crazy character stuff going on.
approach 2
Since SafeString does the escaping I got rid of that...
I changed the context in view.py to :
'legs': json.dumps(legs),
**snipped for brevity**
error on js:
SyntaxError: JSON.parse: expected property name or '}'
This is expected.
Approach 3
I changed the context in view.py to :
'legs': legs_json,
**snipped for brevity**
error on js:
SyntaxError: JSON.parse: expected property name or '}'
This is also expected
Approach 4
I changed the context in view.py to :
'legs': SafeString(legs_json),
**snipped for brevity**
Then I get the python error:
UnicodeEncodeError at /set_route/offer/0/
'ascii' codec can't encode character u'\xa9' in position 127: ordinal not in range(128)
Stuff I've thought of
Since I can stringify and parse the object post POST, and I can parse the resulting JSON in the subsequent view I assume that either: the POSTed JSON isn't coming through correctly (in which case some middleware is to blame), or the Python json module encodes things differently to how JSON.js expects it to (this would be very disconcerting indeed).
The Question
Are any of my assumptions about the workings of JSON in terms of Django and JavaScript Correct?
If Yes: How do I get around the issue and get legs
safely instantiated?
If No: What's actually going wrong? And how can I get legs
safely instantiated?
Given that JSON is by definition valid Javascript, you can simply dump it into the template as a JS data structure, rather than a string which you then parse.
window.legs_json = {{legs}};