1

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?

1 Answer 1

1

Instead of passing the actual dict to the template, JSON encode it first:

import json
json.dumps(dict)
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.