使用Jinja2渲染页面时发送JSON对象

时间:2014-04-13 20:06:24

标签: javascript python google-app-engine webapp2

我使用Google App Engine在webapp2中编写了一个应用。我想渲染的页面需要使用后端的一些变量。如何将变量从Python传递到Javascript作为JSON对象?

谢谢!

2 个答案:

答案 0 :(得分:0)

//define your values dictionary

template_values = {"name": name,
               "other_value" : other values}

//render your template

template = jinja_environment.get_template("path to my html file")
return self.response.write(template.render(template_values))

然后在您的HTML中,您可以使用{{name}}

答案 1 :(得分:0)

在模板文件中,您需要执行以下操作:

 <script>
        var your_js_object = {{your_json_dump}}
 </script>

这并不理想,因为这意味着your_js_object是全局的,但您可以访问js文件中的对象。