The problem is that the string representation of the array is not valid JavaScript. u' at the beginning is not good. It:
[u'Afghanistan', u'Japan', u'United Arab Emirates']
should be as follows:
['Afghanistan', 'Japan', 'United Arab Emirates']
You have two options. In the view function, encode it as JSON there:
render_to_response('my_view.html', { 'visitedCountriesList' : json.dumps(visitedCountriesList) })
or create a filter that you can use. See this for an example. Then use is simple:
<script> $(function() { var vCountries = {{ visitedCountriesList|jsonify }}; }); </script>
source share