python - 如何从 Django 模板中的 staticfiles_dirs 访问文件?

标签 python django django-templates static-files

在我的 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 文件?

最佳答案

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

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

关于python - 如何从 Django 模板中的 staticfiles_dirs 访问文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16762637/

相关文章:

python - "CSRF verification failed"尝试上传文件或使用文件浏览器为 Django 创建文件夹时

Django 查询以获取用户最喜欢的帖子?

python - 在 python 中使用多个定界符拆分字符串

python - groupby 内迭代排序

django - 使用基于类的 View (CBV) Django 2.1 Python 重定向页面

python - Django 默认表

python - Pandas 跨列求和并将每个单元格与该值相除

python - 在 python 中没有线程的定时器?

javascript - 使用 django 在 html 中向 &lt;script src=..> 添加条件

python - Django 不显示我的模板的 CSS