【问题标题】:Django Static wont LoadDjango静态不会加载
【发布时间】:2017-04-08 02:08:26
【问题描述】:

我正在学习如何使用 django,但在使用静态文件时遇到了困难。

文件(BASE_DIRwebsite,应用程序是 player):

 Website
 └─── player
      └─── static
           └─── style.css
           └─── admin

在设置中:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'player/static')

index.html:

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{ static 'style.css' %}" >

style.css:

body {
    background-color: blue
}

我跑过collectstatic

加载后,显示的 HTML 没有 css。

控制台显示:

[23/Nov/2016 23:33:13] "GET / HTTP/1.1" 200 278
Not Found: /{ static 'style.css' %}
[23/Nov/2016 23:33:13] "GET /%7B%20static%20'style.css'%20%%7D HTTP/1.1" 404      2172

【问题讨论】:

  • 您是否尝试进行设置:1) STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"), ) 2) STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/') 3) STATIC_URL = '/static/'
  • @Hybrid 是的,即使使用这些设置,它仍然找不到文件。
  • 检查模板指令中的拼写是否存在基本错误

标签: django django-templates django-staticfiles


【解决方案1】:

您是否运行了 python manage.py collectstatic

【讨论】:

【解决方案2】:

模板中的指令错误:

{% load static %}

应该是

{% load staticfiles %}

更新

也没有:

{ static 'style.css' %}

但是

{% static 'style.css' %}

基本拼写错误

【讨论】:

  • 我做了这些更改,但它仍然响应:加载资源失败:服务器响应状态为 404(未找到)
猜你喜欢
  • 2012-04-30
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 2016-09-29
  • 2018-02-03
  • 2019-01-21
  • 2017-10-08
  • 2018-11-16
相关资源
最近更新 更多