虽然正确配置了css文件,但仍会收到静态404错误

时间:2018-05-23 10:33:48

标签: css django

我加载了静态并精确配置了bootstrap.min.css的嵌套目录结构。但是,它意外地抛出了一个错误:

"GET /static/forums/bootstrap.min.css HTTP/1.1" 404 1685

index.html:

{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Forum Home Page</title>
    <link rel="stylesheet" href="{% static "forums/bootstrap.min.css" %}" />
  </head>

css未加载,显示为:

enter image description here

应用论坛&#39;文件结构:

forums
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   └── __init__.py
├── models.py
├── static
│   └── forums
│       └── bootstrap.min.css
├── templates
│   └── forums
│       └── index.html
├── tests.py
├── urls.py
└── views.py

还有setting.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.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!

# 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',
]

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 = 'forum.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'forums/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 = 'forum.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/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.11/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.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"), #notice the comma
)

如果我发表评论STATICFILES_DIRS,则会报告相同的错误。

我的代码中可能出现什么问题?

2 个答案:

答案 0 :(得分:1)

如果DEBUG=True,则将un urls.py添加到urlpatterns

urlpatterns = [....] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

对于有前缀的静态URL,STATICFILES_DIRS可以如下

STATICFILES_DIRS = [
   # ...
   ("forums", os.path.join(os.path.dirname(__file__), "../static/forums/")),
]

答案 1 :(得分:1)

forums/bootstrap.min.css文件位于static应用的forums目录中。

因此,您需要在'forums'中加入INSTALLED_APPS,以便应用程序目录staticfiles finder可以找到它。

当您将os.path.join(BASE_DIR, "static")添加到STATICFILES_DIRS时,Django还会查看项目的静态目录。 No such file or directory错误表明此目录不存在。要在运行collectstatic时停止错误,您可以创建目录或将其从STATICFILES_DIRS中删除。