I have javascript in the form <script src="/static/file.js" />
in a Django template. I would like this referenced file.js to get the {{ STATIC_URL }}
and other template variables.
我在Django模板中以的形式使用javascript。我希望这个引用的file.js获取{{STATIC_URL}}和其他模板变量。
What would be the best way to give access to file.js
to the template variables?
将file.js访问模板变量的最佳方法是什么?
1 个解决方案
#1
1
Render the desired variables in your main layout file, before you include file.js:
在包含file.js之前,在主布局文件中渲染所需的变量:
<script>
var STATIC_URL = '{{ STATIC_URL }}';
</script>
<script src="/static/file.js" />
#include other scripts here, STATIC_URL will be available as regular variable
#1
1
Render the desired variables in your main layout file, before you include file.js:
在包含file.js之前,在主布局文件中渲染所需的变量:
<script>
var STATIC_URL = '{{ STATIC_URL }}';
</script>
<script src="/static/file.js" />
#include other scripts here, STATIC_URL will be available as regular variable