Django模板变量和Javascript [英] Django Template Variables and 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.
推荐答案
code> {{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屋!