静态文件不显示在Django模板中

2024-04-24 12:35:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的Django模板中显示带有{STATIC{URL}}标签的images/js/css。从理论上讲,这是因为在浏览器中,当我看到打印的代码时,所请求文件的路径类似于“/static/images”/布拉布拉.jpg". 但是文件没有显示,所以我想问题出在Django项目中。在

我的设置如下:

STATIC_ROOT = '/absolute/path/to/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    '/absolute/path/to/static'
)

在我的网址.py公司名称:

^{pr2}$

但我不知道为什么静态目录中的所有文件都不显示在模板中。在

有什么建议吗?在

谢谢朋友们。在


Tags: 文件topathdjango模板urlusejs
1条回答
网友
1楼 · 发布于 2024-04-24 12:35:57

首先,你的设置好像有错误。您应该理解django.contrib.staticfilescollectstatic命令:

Collects the static files into STATIC_ROOT. [...] Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.

因此,STATICFILES_DIRS包含{a4}似乎是个错误,因为STATICFILES_DIR的内容是由collectstatic复制到STATIC_ROOT中的。在

url不是必需的。您应该:

如果有疑问,findstatic命令也可能有用。在

在django.contrib.staticfiles一开始看起来有点困惑,但当你完全理解它时,你就会爱上它<;3

相关问题 更多 >