Django找不到项目级的CSS文件

时间:2015-06-25 23:42:50

标签: django

我是Django的新手,我正在努力了解django如何处理静态文件。我正在按照教程建立一个博客网站。这是我的项目布局:

-myblog
  -manage.py
  -myblog
    -settings.py
    -urls.py
  -blog
    -views.py
    -urls.py
    -views.py
    -static
      -blog
        -css
          -style.css
    -templates
      -blog
        -blog.html
  -static
    -css
      -style.css
  -templates
    -index.html

但index.html文件找不到它的css文件,在index.html文件中,我有

{% load staticfiles %}
<link href="{% static 'css/style.css' %}" rel="stylesheet">

但它可以在app目录中找到css文件

{% load staticfiles %}
<link href="{% static 'blog/css/style.css' %}" rel="stylesheet">

我的settings.py文件:

"""
Django settings for myblog project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
    os.path.join(BASE_DIR, 'blog/templates'),
    )


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'myblog.urls'

WSGI_APPLICATION = 'myblog.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myblog',
        'USER': 'root',
        'PASSWORD': 'q',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'US/Eastern'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
#
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
    # os.path.join(BASE_DIR, 'static/'),
    os.path.join(BASE_DIR, 'blog/static/'),
    )

谁能告诉我这里发生了什么?提前谢谢!

2 个答案:

答案 0 :(得分:1)

您可以拥有任意数量的静态文件文件夹,只需a)相应地命名这些文件夹(整个项目的静态文件和单独的每个应用程序)和b)provide静态文件夹的路径在settings档案中。

在最新的django(1.8)项目中,我有以下设置

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'app_name_here/static/'),
    os.path.join(BASE_DIR, 'static/'),
    os.path.join(BASE_DIR, 'static/bootstrap-3.3.4'),
)

没有定义STATIC_ROOT

答案 1 :(得分:0)

Django无法检测项目文件夹中的目录。如果你在myblog中保留static和templates文件夹,它将顺利运行。

-myblog
  -myblog
    -settings.py
    -urls.py
  -blog
    -views.py
    -urls.py
    -views.py
    -static
      -blog
        -css
          -style.css
    -templates
      -blog
        -blog.html
  -static
    -css
      -style.css
  -templates
    -index.html