2016-11-23 201 views
0

我正在學習如何使用django,但使用靜態文件時遇到困難。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 
+0

你嘗試進行設置:1)'STATICFILES_DIRS =( os.path.join(BASE_DIR, 「靜態」), )' 2)' STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles /')' 3)'STATIC_URL ='/ static /'' – Hybrid

+0

@Hybrid是的,即使使用這些設置仍然無法找到文件。 – JoshK

+0

檢查模板僞指令中的拼寫是否存在基本錯誤 – Dawid

回答

0

你運行蟒蛇manage.py collectstatic

+0

是的,它將文件添加到靜態目錄 – JoshK

+0

你的'urls.py'中是否有靜態路由https://docs.djangoproject.com/zh/1.10/howto/開發過程中的靜態文件/#服務靜態文件 – Dawid

0

模板錯誤指令:

{% load static %} 

應該

{% load staticfiles %} 

更新

也不:

{ static 'style.css' %} 

{% static 'style.css' %} 

基本拼寫錯誤

+0

我做了這些更改,但仍然響應:無法加載資源:服務器響應狀態爲404(未找到) – JoshK