我正在尝试在我的页面上加载 style.css。
我已经添加了 STATICFILES_DIRS
的路径,如下所示,
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static_cdn")
index.html
{% load staticfiles %}
`<link rel='stylesheet' href='{% static "css/style.css" %}' type='text/css'>`
当我运行 collectstatic
时,它收集了 style.css
。
当我检查元素并打开 style.css
(http://127.0.0.1:8000/static/css/style.css)
的链接时,它给我 not found
消息。
我尝试对 style.css
的路径进行硬编码,但没有成功。
我创建了另一个示例元素并按照相同的步骤操作并成功加载了 style.css
。
当我检查元素并打开 style.css
的链接时,它会显示 html 代码。
我真的很无奈。非常感谢任何帮助。
编辑
setting.py template settings:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
目录结构:
|myproject
|----- MyApp/
|---- myProject/
|---- static/
|---- static_cdn/
|----manage.py
最佳答案
如果我没记错的话,collectstatic
命令只用于生产。所以采集到的状态文件不影响开发。
在您的元素结构中,style.css
的路径是:
Salmon/static/Salmon/style.css
。
因此您应该找到具有 URL 的文件:
http://127.0.0.1:8000/static/Salmon/style.css
这应该等同于模板中的 {% static "Salmon/style.css"%}
。
关于html - 无法在 Django 1.9 中加载 style.css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52447948/