Reputation: 3860
A dict in which the string values are unicode string like {'name': u'nav', 'school': u'mps'}
In javascript, i'm trying access that dict like
<script>
var poi = {{ dict }} ........... //Line #1
alert( poi.name )
</script>
while rending the html page line #1 is rendered as (error):
{'lastname': u'yadav', 'age': 23, 'id': 2, 'firstname': u'sahenky'}
One python solution is to encode the unicode string into some other format like ascii or utf-8(preferred)
str = u'nav'
str.encode('utf-8, 'ignore')
>>str
nav
Is there any elegant way to encode unicode string in python or javascript?
Upvotes: 1
Views: 1803
Reputation: 55303
Instead of passing the actual dict to the template, JSON encode it first:
import json
json.dumps(dict)
Upvotes: 1