1

我想知道是否可以将views.py中的render_to_response中的变量传递给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 页面中使用它。

4

2 回答 2

0

<script></script>没有好的解决方案是在 html 文件之间编写代码

也许在my_script.html

<script>
    function WillHappen()
    {
        var n = noty({
            text: 'You're {{var}}!',
            type: 'warning',
            dismissQueue: false,
            layout: 'center',
            theme: 'defaultTheme'
        })

    }
</script>

但是,这样好吗?

于 2013-05-14T13:22:49.647 回答
0

好吧,您可以将所有变量都放在脚本标签中,并通过其他 javascript 文件访问它们,例如:

<script>
var test = {{var}} || false;
</script>

在其他 js 文件中:

if (window.test) {
//
}
于 2017-08-30T11:19:14.483 回答