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

后端 未结 6 1671
挽巷
挽巷 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:55

    To add up on the selected answer, I have been testing a new option that is working too using jinja2 and flask:

    @app.route('/')
    def my_view():
        data = [1, 2, 3, 4, 5]
        return render_template('index.html', data=data)
    

    The template:

    
    

    the output of the rendered template:

    
    

    The safe could be added but as well like {{ data | tojson | safe }} to avoid html escape but it is working without too.

提交回复
热议问题