【问题标题】:Django template variables in linked javascript链接的javascript中的Django模板变量
【发布时间】:2012-03-06 13:41:47
【问题描述】:

我在 Django 模板中有 <script src="/static/file.js" /> 形式的 javascript。我想要这个引用的 file.js 来获取 {{ STATIC_URL }} 和其他模板变量。

file.js 访问模板变量的最佳方式是什么?

【问题讨论】:

标签: javascript django django-templates


【解决方案1】:

在包含 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

【讨论】:

    猜你喜欢
    • 2011-07-25
    • 2021-06-14
    • 2016-04-22
    • 2013-05-26
    • 2021-06-19
    相关资源
    最近更新 更多