【发布时间】:2012-03-06 13:41:47
【问题描述】:
我在 Django 模板中有 <script src="/static/file.js" /> 形式的 javascript。我想要这个引用的 file.js 来获取 {{ STATIC_URL }} 和其他模板变量。
让file.js 访问模板变量的最佳方式是什么?
【问题讨论】:
标签: javascript django django-templates
我在 Django 模板中有 <script src="/static/file.js" /> 形式的 javascript。我想要这个引用的 file.js 来获取 {{ STATIC_URL }} 和其他模板变量。
让file.js 访问模板变量的最佳方式是什么?
【问题讨论】:
标签: javascript django django-templates
在包含 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
【讨论】: