html - 无法在 Django 1.9 中加载 style.css
问题描述
我正在尝试在我的页面上加载 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
命令仅用于生产。所以收集的statifiles不会影响开发。
在您的项目结构中,路径style.css
为:
Salmon/static/Salmon/style.css
.
所以你应该找到带有 URL 的文件:
http://127.0.0.1:8000/static/Salmon/style.css
这应该等同{% static "Salmon/style.css" %}
于您的模板。
推荐阅读
- elixir - 使用 Ueberauth 不断收到 Cross-Site Request Forgery 错误
- haskell - 解析json时如何从多个GADT构造值?
- python - 有没有办法获取函数的运行次数
- flutter - Flutter 如何在 GridView.count 中获取 onTap() 命令
- angular - 角度 12 中带有 _observableOf(null) 的 Swagger 自动生成的文件问题?
- google-chrome-extension - Chrome 扩展程序 - 在后台使用正文数据监控网络流量
- macos - 如何在 macOS 上打开多个 Alacritty 实例?
- latex - 乳胶禁忌包
- javascript - jquery - 无法将列表元素从右侧移回不知道为什么
- go - goroutine 和 channel 的问题