Django 模板变量和 Javascript [英] Django Template Variables and Javascript

查看:35
本文介绍了Django 模板变量和 Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 Django 模板渲染器渲染页面时,我可以传入一个包含各种值的字典变量,以使用 {{ myVar }} 在页面中操作它们.

When I render a page using the Django template renderer, I can pass in a dictionary variable containing various values to manipulate them in the page using {{ myVar }}.

有没有办法在 Javascript 中访问相同的变量(也许使用 DOM,我不知道 Django 如何使变量可访问)?我希望能够根据传入的变量中包含的值使用 AJAX 查找来查找详细信息.

Is there a way to access the same variable in Javascript (perhaps using the DOM, I don't know how Django makes the variables accessible)? I want to be able to lookup details using an AJAX lookup based on the values contained in the variables passed in.

推荐答案

{{variable}} 被直接替换到 HTML 中.做一个查看源码;它不是变量"或类似的东西.它只是渲染文本.

The {{variable}} is substituted directly into the HTML. Do a view source; it isn't a "variable" or anything like it. It's just rendered text.

话虽如此,您可以将这种替换放入您的 JavaScript 中.

Having said that, you can put this kind of substitution into your JavaScript.

<script type="text/javascript"> 
   var a = "{{someDjangoVariable}}";
</script>

这为您提供了动态"javascript.

This gives you "dynamic" javascript.

这篇关于Django 模板变量和 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆