Javascript 使用Jinja2呈现页面时发送JSON对象
我有一个应用程序是用谷歌应用程序引擎在Javascript 使用Jinja2呈现页面时发送JSON对象,javascript,python,google-app-engine,webapp2,Javascript,Python,Google App Engine,Webapp2,我有一个应用程序是用谷歌应用程序引擎在webapp2中编写的。我想要呈现的页面需要使用后端的一些变量。如何将变量作为JSON对象从Python传递到Javascript 谢谢 //define your values dictionary template_values = {"name": name, "other_value" : other values} //render your template template = jinja_environme
webapp2
中编写的。我想要呈现的页面需要使用后端的一些变量。如何将变量作为JSON对象从Python传递到Javascript
谢谢
//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},您需要执行以下操作:
<script>
var your_js_object = {{your_json_dump}}
</script>
var your_js_object={{your_json_dump}
这并不理想,因为这意味着
您的_js_对象
是全局的,但您可以访问js文件中的对象。您可以解释Jinja2是如何关联的以及您尝试了什么吗?我尝试使用{name}
将输入标记的值设置为Python中的值,这是Jinja的模板化功能。