How to pass a list from Python, by Jinja2 to JavaScript

后端 未结 6 1673
挽巷
挽巷 2020-11-27 10:21

Let\'s say I have a Python variable:

list_of_items = [\'1\',\'2\',\'3\',\'4\',\'5\']

and I pass it to Jinja by rendering HTML, and I also h

6条回答
  •  青春惊慌失措
    2020-11-27 10:48

    You can do this with Jinja's tojson filter, which

    Dumps a structure to JSON so that it’s safe to use in

    DON'T use json.dumps to JSON-encode variables in your Python code and pass the resulting JSON text to your template. This will produce incorrect output for some string values, and will expose you to XSS if you're trying to encode user-provided values. This is because Python's built-in json.dumps doesn't escape characters like < and > (which need escaping to safely template values into inline

提交回复
热议问题