请帮助理解我做错了什么。
我在我的settings.py中:
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = os.path.join(PROJECT_ROOT, 'static').replace('\\','')+'/'
在index.html中:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static "/css/table.css" %}">
但我仍然有错误404:
"GET /var/cardsite/cardsite/static/css/table.css HTTP/1.1" 404 1696
我有这个文件:
ls -la /var/cardsite/cardsite/static/css/table.css
-rw-r--r-- 1 root root 77 Sep 25 16:15 /var/cardsite/cardsite/static/css/table.css
那是怎么回事?
P.S。我的项目存储在“/ var / cardsite”上,我想在每个应用程序上创建静态文件夹,例如默认应用程序“cardsite”
感谢
答案 0 :(得分:1)
阅读此Django_Docs
在使用静态文件之前,您还必须设置STATIC_ROOT
选项,这是一些帮助
将其添加到您的代码中:
STATIC_URL = os.path.join(PROJECT_ROOT, 'static').replace('\\','')+'/'
# Here you can add all the directories from where you want to use your js, css etc
STATICFILES_DIRS = [
# This can be same as the static url
os.path.join(PROJECT_ROOT, "static"),
# also any other dir you wanna use
"/any/other/static/path/you/wanna/use",
]
# This is the static root dir from where django uses the files from.
STATIC_ROOT = os.path.join(PROJECT_ROOT, "static_root")
您还需要在urls.py
文件中指定它,只需将以下代码添加到urls.py
文件中:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
添加后,运行命令:
python manage.py collectstatic
这会将您需要的所有静态复制到静态根目录。