Project Overview
I am currently developing an application aimed at assisting users in finding rides. My tech stack includes Django, Python 2.7, and integration with Google Maps and Directions APIs.
Within a specific view, I present a map where users can select multiple locations along with arrival and departure times. Subsequently, I utilize the Google Directions API to retrieve several alternate routes between the specified points in JSON format. After parsing and modifying this data, it is saved into a distinct structure termed legs
. So far, everything functions smoothly.
Upon clicking the 'next' button, the legs
structure is stringified and posted to the subsequent view as part of a form using the following code:
document.getElementById('legs_field').value = JSON.stringify(window.legs);
It's also worth mentioning that the below line executes without any issues:
JSON.parse(JSON.stringify(window.legs));
The Challenge
I'm encountering difficulties accessing the legs
object within the JavaScript framework of the next view. Therefore, my primary question revolves around the methodology to make the identical legs
data structure accessible in the consecutive view.
Attempted Solutions
Approach 1:
In the 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**
})
In the template:
window.legs_json = '{{legs}}';
**snipped for brevity**
In the JavaScript:
window.legs = JSON.parse(window.legs_json);
Error in JavaScript:
SyntaxError: JSON.parse: bad escaped character
I have previously used a similar technique involving JSON handling which worked effectively. Therefore, there seems to be some irregular character-related issue.
<approach 2
By removing SafeString which performs escaping, I updated the context in view.py
to:
'legs': json.dumps(legs),
**snipped for brevity**
Error in JavaScript:
SyntaxError: JSON.parse: expected property name or '}'
This outcome was anticipated.
Approach 3
Keeping the changes in view.py
consistent with:
'legs': legs_json,
**snipped for brevity**
Error in JavaScript:
SyntaxError: JSON.parse: expected property name or '}'
As expected, the error persisted
Approach 4
To address the issue, in the view.py
context:
'legs': SafeString(legs_json),
**snipped for brevity**
Subsequently, I encountered a Python error:
UnicodeEncodeError at /set_route/offer/0/
'ascii' codec can't encode character u'\xa9' in position 127: ordinal not in range(128)
Deliberations
Given my ability to stringify and parse the object post-POST request, as well as being able to parse the resultant JSON in the ensuing view, I suspect either the transmitted JSON via POST is flawed (potentially due to middleware), or the encoding by the Python JSON module differs from the anticipations of JSON.js, presenting a concerning scenario.
Inquiry
With respect to JSON functionality within Django and JavaScript, are my presumptions accurate?
If affirmative, what workaround exists to appropriately instantiate legs
?
If negative, where lies the actual concern? Furthermore, how might I securely instantiate legs
?