我的结构看起来像这样:
myproject/
app1/
static/
css/
style.css
myproject/
settings.py
static/
css/
style.css
db.sqlite3
在 settings.py 中我有:
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..'))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# blank
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
在我的 index.html 中我有:
<link rel="stylesheet" type="text/css" href="/static/css/style.css" />
我正在尝试从 myproject/static 文件夹加载 css,但每当我加载 index.html 时,它都会查看/app1/static/css/style.css,但 db.sqlite3 正在工作。
我什至尝试删除 app/static 文件夹,但没有用。我做错了什么?
最佳答案
添加
{% load staticfiles %}
在模板的顶部<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}" />
在settings.py
,
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
不需要 STATIC_ROOT
,如果您在本地主机上运行。
关于css - 静态文件未在 django 1.6 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22590218/