0

I loaded static and configured finely the nested directory structure for bootstrap.min.css. However, it unexpectedly throws an error:

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

The 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>

The css was not loaded ant it displayed:

enter image description here

The app forums' file structure:

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

And the 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
)

If I comment out STATICFILES_DIRS it reports the same error.

What might be the problem in my code?

6
  • 1
    please try python manage.py collectstatic Commented May 23, 2018 at 10:37
  • FileNotFoundError: [Errno 2] No such file or directory: '/Users/me/Desktop/Django/forum/static' Commented May 23, 2018 at 10:40
  • 1
    Are you using runserver in development with DEBUG=True? Is 'forums' in INSTALLED_APPS. Please show the layout of the project directory as well. Commented May 23, 2018 at 10:49
  • 1
    If DEBUG=True, add following to urls.py + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) Commented May 23, 2018 at 10:52
  • I forgot to add forums to INSTALLED_APPS, could you please transmit your comment to answer. @Alasdair Commented May 23, 2018 at 10:55

2 Answers 2

1

The forums/bootstrap.min.css file is in the static directory of your forums app.

Therefore you need to include 'forums' in INSTALLED_APPS so that the app directories staticfiles finder can find it.

When you add os.path.join(BASE_DIR, "static") to STATICFILES_DIRS, Django will also look in the static directory of your project. The No such file or directory error suggests that this directory doesn't exist. To stop the error when running collectstatic you can either create the directory or remove it from STATICFILES_DIRS.

Sign up to request clarification or add additional context in comments.

2 Comments

static may be invented to save life but it eventually haunting complicated. When I comment out, it report ` STATIC_ROOT setting to a filesystem path.`, I'd like to forget it and continue my learning.
Since you have 'django.contrib.staticfiles' in your INSTALLED_APPS, the runserver will take care of serving static files for you. You don't have to set STATIC_ROOT or run collectstatic until you deploy your project into production.
1

If DEBUG=True then un urls.py add to urlpatterns

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

And for static URL which do have a prefix, STATICFILES_DIRS could be as below

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

1 Comment

The OP already has 'django.contrib.staticfiles' in their INSTALLED_APPS, therefore they shouldn't need to use += static(...) in their urls.py.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.