css - 静态文件未在 django 1.6 中加载

标签 css django

我的结构看起来像这样:

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 文件夹,但没有用。我做错了什么?

最佳答案

  1. 添加 {% load staticfiles %}在模板的顶部

  2. <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/

相关文章:

javascript - 销毁事件后工具提示不会从 DOM 中删除

javascript - Django POST 请求中禁止的 CSRF token 丢失或不正确,即使我在表单中有 csrf token

sql - Django:模型设计,ManyToMany 属性字段?

django - 在模板中声明变量

python - Django 的性能问题

python - Django:根据预保存信号调整图像大小

CSS 子菜单 z-index 问题。出现在主菜单后面

javascript - 我如何更改此天气预报小部件在我的网站上的位置?

css - 如何在 CSS 中制作这样的进度条

css - 使用下拉菜单时导航栏会变高(仅限移动设备)