html - 无法在 Django 1.9 中加载 style.css

标签 html css django python-2.7 django-1.9

我正在尝试在我的页面上加载 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

https://github.com/prafullarkamble/UnableToLoadStyle.css/

最佳答案

如果我没记错的话,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/

相关文章:

javascript - Jquery 必填字段 - 无插件

html - 布局 3 列和 2 列高度不匹配

javascript - onclick ="location.href=' link.html'"在 Safari 中不加载页面

css - WooCommerce 产品未在网格中显示

python - 能够将字段保存到变量中并使用到 queryset.value() 中吗? Django

php - 如何检查 session 是否已启动并重定向用户?

javascript - 如何制作具有高亮样式的事件菜单项?

jquery - 扩展子 block 时增加父 block 高度(绝对)

python - 尝试同步数据库时出现类错误(Python Django)

django - 如何在 PyCharm 中重命名 Django 项目?