我想知道是否有可能将render_to_response中的变量从views.py传递到script.js,
render_to_response('appname/basic.html',{'var':somevariable})
我基本上想访问HTML页面外的变量{{var}},因为我的script.js使用如下:
function WillHappen()
{
var n = noty({
text: 'You're {{var}}!',
type: 'warning',
dismissQueue: false,
layout: 'center',
theme: 'defaultTheme'
})
}
如果我将Javascript放入HTML页面,但我想将它们分开,因为我可能会在很多HTML页面中使用它。
一个不太好的解决方案是在html文件中编写<script></script>
之间的代码
可能在my_script.html
中:
<script>
function WillHappen()
{
var n = noty({
text: 'You're {{var}}!',
type: 'warning',
dismissQueue: false,
layout: 'center',
theme: 'defaultTheme'
})
}
</script>
但是,这样好吗?
你可以把所有的变量放在一个script标签里,然后通过其他javascript文件访问它们,比如:
<script>
var test = {{var}} || false;
</script>
在其他js文件中:
if (window.test) {
//
}