0

today I had an issue with my Django project CSS files. My CSS files do not load when I try accessing my page. At first, I thought the issue was only with the Django admin, so I asked this question, however, I think this issue deserves its own question.

My pages load as normal, just without CSS style, and no errors show up. However, in the chrome console, I find this Error:

Refused to apply style from 'https://[mywebsite].com/static/pathToMyCssFile/file.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

I don't understand what it means by "its MIME type ('text/html') is not a supported stylesheet MIME type"? Is it detecting my CSS code as HTML, or the other way round?

my settings.py:

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 3.0.8.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os
import mimetypes


# A Bug is was encountering
mimetypes.add_type("text/css", ".css", True)
# 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/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#######################################' # Im supposed to keep this secret?

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

SECURE_SSL_REDIRECT = True
ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'students_app.apps.StudentsAppConfig',
    '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 = 'mysite.urls'

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


# Database
# https://docs.djangoproject.com/en/3.0/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/3.0/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/3.0/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/3.0/howto/static-files/

# I ran python manage.py collectstatic. Still fails to work
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'

I have also tried this solution.

EDIT: My Admin CSS files return 404

Admin Base.CSS

Admin base.css
Thank you!

Rayyan
  • 182
  • 4
  • 16
  • Try opening the `file.css`. If it is a Django 404 error page, give the screenshot of this page – Talha Quddoos Jul 31 '20 at 02:02
  • @Talha Quddoos No error when I open my app static files anymore thanks to GAEfan's answer. My admin page css files have a 404 error however – Rayyan Jul 31 '20 at 13:46

2 Answers2

1

It's supposed to be text/css mimetype, like: <link rel="stylesheet" type="text/css" href="mystyle.css">

GAEfan
  • 11,244
  • 2
  • 17
  • 33
  • Thank You! I had forgotton that in my template html. Now my pages seem to work. However, my admin page is still not working. – Rayyan Jul 31 '20 at 13:36
  • Should I delete this question since it works now? The admin still doesn't work but that is my other question – Rayyan Jul 31 '20 at 13:43
  • 1
    No. You accept this answer, then ask the new question – GAEfan Jul 31 '20 at 15:44
-1

On my Django application which I am hosting on AWS elastic beanstalk , I just have to add text/css in my CSS links it solves all issues