Django Template - Convert a Python list into a JavaScript object

后端 未结 4 1509
谎友^
谎友^ 2020-12-30 02:19

I am working on a Django / Python website. I have a page where I want to display a table of search results. The list of results is passed in to the template as normal.

4条回答
  •  感动是毒
    2020-12-30 03:01

    How about a filter that dumps a Python value to JSON? Here's a sample implementation:

    http://djangosnippets.org/snippets/201/

    Since a JSON value also happens to be a valid right-hand side for a Javascript assignment, you can simply put something like...

    var results = {{results|jsonify}};
    

    inside your script.

提交回复
热议问题