D
D
dimasik24na72020-05-16 16:20:28
Django
dimasik24na7, 2020-05-16 16:20:28

Django how to resolve 404 error with files from static?

I recently started learning the Django framework and created my first project by following the tutorial and including the style file in base.html

<link rel="stylesheet" type="text/css" href="{% static 'style/style.css' %}">
encountered error [16/May/2020 17:34:41] "GET /static/css/main.css HTTP/1.1" 404 1662 on the command line when starting the server.

Contents of the settings.py file
import os, sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps'))
DEBUG = True

ALLOWED_HOSTS = []
INSTALLED_APPS = [
    'grappelli',
    '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 = 'buysellhome.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_ROOT)
        ],
        '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',
                'django.template.context_processors.request'
            ],
        },
    },
]

WSGI_APPLICATION = 'buysellhome.wsgi.application'
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
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',
    },
]
LANGUAGE_CODE = 'ru-RU'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True
STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')


Project Structure
5ebfe8753687a034996091.png

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Sobolev, 2020-05-16
@Sobolev5

I ran into a similar problem while developing on a laptop.
My decision.

LOCAL_MACHINE_DEV = os.getenv("LOCAL_MACHINE_DEV") == "1"

if LOCAL_MACHINE_DEV:
    STATICFILES_DIRS = ["/my/project/static"]
else:
    STATIC_ROOT = os.path.join(PROJECT_PATH, "static")

A
alternativshik, 2020-05-16
@alternativshik

in urls what? Need to add

if settings.DEBUG:
        urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

S
siarheisiarhei, 2020-05-16
@siarheisiarhei

and, why do you need such a porridge-confusion - well, in everything ....?

M
Mikhail Goryunov, 2022-04-07
@gormih

What do you think it returns:
os.path.dirname(__file__)
It will return the location of the settings.py file , which is one level up from static .
As a result, you will get a directory that is, as it were, in the directory along with the settings.py file, and it is, as it were, very different from the desired one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question