开发者

Django - Template Vars in static JS files

开发者 https://www.devze.com 2023-03-13 23:48 出处:网络
In Django, lets say I want to import a javascript file using script src=\"blah.js\" 开发者_如何学C

In Django, lets say I want to import a javascript file using script src="blah.js"

开发者_如何学C

I pass in a template variable {{ business }}. Is there a way for the static javascript file blah.js to access {{ business }}? I can do it with javascript in the html templates but once I call a static script it doesn't work. It seems like it'd be best to separate javascript files I call in multiple templates into a javascript file so I thought it might be possible.


You redesign your code in such a way that in the templates you make calls to javascript functions with the template variables as arguments:

<script type="text/javascript">
    x = foo({{ business }}, {{ some_other_variable }});
    alert(x);
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消