【问题标题】:How to access file from staticfiles_dirs within django template?如何从 django 模板中的 staticfiles_dirs 访问文件?
【发布时间】:2013-05-21 16:30:57
【问题描述】:

在我的 settings.py 中,我已经设置了 staticfiles_dirs

STATICFILES_DIRS = (
    os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'static')).replace('\\','/') 
)

在 myproject/static 文件夹中,我有一个名为 jquery-1.10.0.js 的文件。我试图像这样在我的应用程序模板中加载文件

{% load static %}

<script type="text/javascript" src="{% static "jquery-1.10.0.js" %}"></script>

但我无法从我的模板中访问 jquery 函数,如果我转到 {% static "jquery-1.10.0.js" %} 返回的 url,即 /static/jquery-1.10.0.js,我会看到一个页面显示发生服务器错误.请联系管理员。

如何从模板中访问 jquery 文件?

【问题讨论】:

标签: python django django-templates static-files


【解决方案1】:

这有点傻,但我无法访问静态文件的原因是因为我没有在 staticfiles_dirs 元组的第一个元素的末尾添加逗号。

首先它不起作用的原因是因为最后需要一个逗号,以便 python 将 staticfiles_dirs 变量识别为元组。

【讨论】:

    猜你喜欢
    • 2019-05-24
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2016-07-20
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多