Django 4.2 Unable to read my CSS file located within the static/css folder

Hi all,

I have an issue with my css stylesheet file being read when I run the project. CSS file is not being read at all. I have a JS folder with JavaScript files and that is being read but nothing for the CSS folder where the Stylesheet is located.

Is this a bug with Django 4.2? I am using Python 3.9. Could this be an issue? Reason for Python 3.9, I had a struggle accessing MySQL database, but that is a topic for another day.

The project is still in development stage, so DEBUG mode is set to True at the moment.

Please see below the current settings in Settings.py.

Below is the snippet for index.html, extending the layout.html where the link to the CSS file is included.
Home Page Extending Layout

Layout.html for the link to the CSS file.

Directory where the css file is located.
Statics Folder

Many thanks,
Jurgens

Note: Please do not post screenshots of code, etc. copy/paste the text into the body of your message, between lines of three backtick- ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

Without being able to read your code, I’m going to work under the assumption that no - there’s no bug related to this in 4.2.

Also, please indicate whether a request is being made to the server for this file, and if the server is returning a 404; or if there’s no request being seen for this file.

Hi Ken,

Thanks for getting back to me.

I Will try my best to get my code below. I hope the below paints a better picture.

The snippet below is the layout.html where the Stylesheet has been referenced.

{% load static %}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">

    *<link rel=”stylesheet” href="{% static 'css/otjiherero.css' %}" type="text/css">*

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

    <link rel="stylesheet" type="text/css" href="{% static "fontawesome/css/all.min.css" %}"/>

    <script src="{% static 'js/clock.js' %}"></script>

    <script src="{% static 'js/greetings.js' %}"></script>

</head>
<body>

        <nav class="navbar navbar-expand-lg navbar-dark bg-dark bg-spring text-capitalize">
More code has been excluded.
</body>
</html>

Below is the Settings.py code

import os
from pathlib import Path

from django.contrib import staticfiles

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-iu8rcz27hx7^+&yn0ke=0$2*a09k5!q5cs$vwju1i1b#7a)pf*'

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

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'django.contrib.staticfiles',
    'omahi.apps.OmahiConfig',
    'django_static_fontawesome',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',

]

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / '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 = 'otjiherero_django_v1.wsgi.application'

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'user_db',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '3306',
        # 'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# Password validation
# https://docs.djangoproject.com/en/4.2/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/4.2/topics/i18n/

LANGUAGE_CODE = 'en-GB'

TIME_ZONE = 'Europe/London'

USE_I18N = True

USE_TZ = True

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


STATIC_URL = "static/"

# STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
#
STATICFILES_DIRS = [
    BASE_DIR / "static/",
]

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


Below is the output from the cmd line after running the: python manage.py runserver - this is after a hard refresh. It looks like all the files are being read but the stylesheet is not present. The Fontawesome and the .js files all shows the HTTP 200 code

Starting development server at http://127.0.0.1:8006/
Quit the server with CTRL-BREAK.

Not Found: /
[08/Nov/2023 23:41:15] "GET / HTTP/1.1" 404 2179
[08/Nov/2023 23:41:25] "GET /omahi/ HTTP/1.1" 200 7123
[08/Nov/2023 23:41:32] "GET /omahi/ HTTP/1.1" 200 7123
[08/Nov/2023 23:41:33] "GET /static/js/clock.js HTTP/1.1" 200 2482
[08/Nov/2023 23:41:33] "GET /static/fontawesome/css/all.min.css HTTP/1.1" 200 102217
[08/Nov/2023 23:41:33] "GET /static/js/greetings.js HTTP/1.1" 200 552
[08/Nov/2023 23:41:33] "GET /static/fontawesome/webfonts/fa-solid-900.woff2 HTTP/1.1" 200 150020
Not Found: /favicon.ico
[08/Nov/2023 23:41:33] "GET /favicon.ico HTTP/1.1" 404 2230

Code for the urls.py for the app.

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index")

]

The views.py file


from django.shortcuts import render

from .models import Continent, ContinentRegions


# Create your views here.
def index(request):
    return render(request, "omahi/index.html", {
        "continentRegions": ContinentRegions.objects.all()
    })

Main Application’s urls.py file

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
                  path('admin/', admin.site.urls),
                  path("omahi/", include("omahi.urls")),
              ]

Note that you have invalid characters here:

*<link rel=”stylesheet” href="{% static 'css/otjiherero.css' %}" type="text/css">*
           ^          ^

You have “smart quotes” instead of regular quotes.

1 Like

Hi Ken,

Thanks for getting back to me.

I can confirm the smart quotes were the issue. The application is running perfectly fine now.

Many thanks for your assistance.