How To Read Python List In Javascript [in A Django Template]
I'm programming in oTree (which is a Django based environment for social experiments) and I have the following problem. I defined some lists in Python and I'd like to import them a
Solution 1:
It sounds like your data is already JSON, otherwise you would be getting single quotes and u
prefixes. So the only issue is Django autoescaping; you can disable it with the safe
filter:
var filtered_elements = {{ array|safe }};
Solution 2:
Your data should be JSON, instead of putting the Python list into the contact directly, put "array": json.dumps(array)
in the context dictionary.
The JSON string doesn't need HTML escaping inside a tag, but it does need JS escaping! Otherwise some string may include something like </script><script>absolutely anything goes here...
to run arbitrary JavaScript, if the JSON contains user data.
So use |escapejs
:
var filtered_elements = {{ array|escapejs}};
Post a Comment for "How To Read Python List In Javascript [in A Django Template]"