I am trying to create a javascript list from a python list using jinja2. My current implementation is this:
var go_words = [{{"\"" + user.names | join('\",\"') + "\""}}]
which yields:
var go_words = ['Name 1', ...]
For some reason the "
character is not being interpreted correctly and thus my script is failing. Is there anyway to fix this? Note that my code for this javascript is in an html <script>
tag which is included from another html template.
Even an inline list in the jinja brackets yields and incorrect list
var go_words = {{["test", "test1", "test2"]}}
var go_words = ['test', 'test1', 'test2']
You can use the safe
filter to prevent Jinja from escaping templated values. For example:
var goWords = {{ user.names|safe }};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.