未在django 1.6中加载静态文件

时间:2014-03-23 11:23:06

标签: 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文件夹,但它不起作用。我做错了什么?

2 个答案:

答案 0 :(得分:0)

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

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

  3. settings.py

    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    
    STATIC_URL = '/static/'
    
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, "static"),
    )
    

    如果你在localhost上运行,则不需要STATIC_ROOT

答案 1 :(得分:0)

您忘记添加静态标记以在模板中加载文件:

在你的html文件的开头你必须放

{% load staticfiles %}

然后在路径中添加静态标记

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

请参阅django中的文档:

https://docs.djangoproject.com/en/1.6/howto/static-files/

对刚刚回答你的那个人来说,他的速度更快了;)