Reputation: 22083
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:
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?
Upvotes: 0
Views: 119
Reputation: 308859
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
.
Upvotes: 1
Reputation: 635
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/")),
]
Upvotes: 1