无法将CSS文件加载到Django 1.10中

时间:2017-02-13 05:28:01

标签: django

我是Django的新手。我正在开发一个包含CSS的动态网站。 我正在使用模板继承。当我尝试包含CSS文件时,我收到CSS文件的“404 Page not found”错误。请帮帮我。

这是我的项目结构:

这是我的urls.py文件:

from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views

urlpatterns = [
    url(r'^courses/', include('courses.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.hello_world),
]
urlpatterns += staticfiles_urlpatterns()

这是我的settings.py文件:

import os

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '5%#*z=7o5iap!lnr7(%*(2rsl#*b-ufjy!ia$8z08d(6d8!j-#'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'courses',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'learning_site.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates',],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'learning_site.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'
STATIC_FILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

这是我的layout.html文件:

    {% load static from staticfiles %}
<html>
  <head>
    <title>
        {% block title %}{% endblock title %}
    </title>
    <link rel='stylesheet' href="{% static 'css/layout.css' %}">
  </head>
  <body>
    {% block content %}print something for fun...{% endblock content %}
  </body>

</html>

这是我的home.html文件:

    {% extends 'layout.html' %}
    {% block title %}Hello World {% endblock title%}

    {% block eontent %}
    <h1>
      welcome!!!
    </h1>
   {% endblock content %}

2 个答案:

答案 0 :(得分:0)

如果你的静态文件夹在你的基础项目应用程序中。 就像是。

django_project (your manage.py is here) -> django_project -> static

然后您的静态文件配置应该是。

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'django_project/static'),
)

您不应该为此对网址进行任何更改。

更新:上述情况似乎并非如此。 您的块标记的模板语法不正确。

你能解决这个问题吗?你不是endblock and blockname

<html>
  <head>
    <title>
        {% block title %}{% endblock %}
    </title>
    <link rel='stylesheet' href="{% static 'css/layout.css' %}">
  </head>
  <body>
    {% block content %}print something for fun...{% endblock %}
  </body>

</html>

更新2:

模板上下文过程也需要像这样。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates',],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                "django.core.context_processors.static",
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

答案 1 :(得分:0)

如果您使用的是Django 1.10

这是STATICFILES_DIRS

的文档

改变你的

STATIC_FILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    'static',
]

在您的基础html中,将{% load static from staticfiles %}更改为{% load static %}

  

STATIC_FILES_DIRS 没有django设置配置名称使用 STATICFILES_DIRS